const grouped = Object.entries(arr.reduce((acc, n) => (
(acc[n.id] ??= []).push(n.name),
acc
), {}));document.body.insertAdjacentHTML('beforeend', grouped
.map(([ k, v ]) => `
<ul id="list-${k}">${v.map(n => `
<li>${n}</li>`).join('')}
</ul>`)
.join('')
);document.body.append(...grouped.map(([ id, names ]) => {
const ul = document.createElement('ul');
ul.id = `list-${id}`;
ul.append(...names.map(n => {
const li = document.createElement('li');
li.textContent = n;
return li;
}));
return ul;
}));
const obj = Object.fromEntries(arr.map(n => [ n.name, n.number ]));
// или
const obj = arr.reduce((acc, n) => (acc[n.name] = n.number, acc), {});
// или
const obj = Object.assign({}, ...arr.map(n => ({ [n.name]: n.number })));function toObj(data, key, val = n => n) {
const getKey = key instanceof Function ? key : n => n[key];
const getVal = val instanceof Function ? val : n => n[val];
const obj = {};
for (const n of data) {
obj[getKey(n)] = getVal(n);
}
return obj;
}const obj = toObj(arr, 'name', 'number');
// {Kolya: '5', Olga: '10'}const charCodes = toObj('abc', n => n.charCodeAt());
// {97: 'a', 98: 'b', 99: 'c'}<input name="xxx" value="69">
<input name="yyy" value="187">
<input name="zzz" value="666">const inputValues = toObj(document.querySelectorAll('input'), 'name', 'value');
// {xxx: '69', yyy: '187', zzz: '666'}
hrefs.filter(n => !/\.pdf$/.test(n))
// или
hrefs.filter(n => !n.endsWith('.pdf'))
// или
hrefs.filter(n => n.split('.').pop() !== 'pdf')
// или
hrefs.filter(n => n.lastIndexOf('.pdf') !== n.length - 4)
// или
hrefs.filter(n => n.slice(-4) !== '.pdf')
// или
hrefs.filter(n => n.replace(/.*\./, '') !== 'pdf')
// или
hrefs.filter(n => n.match(/\.[^.]+$/g) != '.pdf')
const isObject = v =>
v instanceof Object && !Array.isArray(v);const flatObj = obj => Object
.entries(isObject(obj) ? obj : {})
.reduce((acc, [ k, v ]) => (
isObject(v)
? Object.assign(acc, flatObj(v))
: acc[k] = v,
acc
), {});function flatObj(obj) {
const result = {};
for (const stack = isObject(obj) ? [ [ , obj ] ] : []; stack.length;) {
const [ k, v ] = stack.pop();
if (isObject(v)) {
stack.push(...Object.entries(v).reverse());
} else {
result[k] = v;
}
}
return result;
}
В чем разница между поехать в отпуск и футболом?
И в том и в другом случае мы будем носить обувь.
Вот пример: в холодильник можно поставить пиво, а по карточке можно снять деньги в банкомате.
xhr.send(data); и по очереди:// сначала ты
xhr.upload.onprogress // загрузка на сервер
xhr.upload.onload // загрузка на сервер завершена
// потом сервер
xhr.onreadystatechange // xhr.readyState === 2 // HEADERS_RECEIVED // получены заголовки ответа (и только)
xhr.onreadystatechange // xhr.readyState === 3 // LOADING // загрузка ответа сервера
xhr.onprogress // загрузка ответа сервера
xhr.onreadystatechange // xhr.readyState === 4 // DONE
xhr.onload // загрузка ответа сервера завершена
const maxStr = arr.length
? arr.reduce((max, n) => +n < +max ? max : n)
: null;function max(data, key = n => n) {
const getVal = key instanceof Function ? key : n => n[key];
let result = null;
for (const n of data) {
const val = getVal(n);
result = result?.[1] >= val ? result : [ n, val ];
}
return result?.[0];
}
const maxStr = max(arr, Number);