Object.entries(str.split('').reduce((acc, n) => (acc[n] = (acc[n] ?? 0) + 1, acc), {}))
[...[...str].reduce((acc, n) => acc.set(n, -~acc.get(n)), new Map)]
Object.values(str).sort().join('').match(/(.)\1*/g)?.map(n => [ n[0], n.length ]) ?? []
Array.from(new Set(str), n => [ n, str.split(n).length - 1 ])
const result = Object.values(runs.reduce(
(acc, n) => (acc[n.projectId]?.description.push(n), acc),
Object.fromEntries(projects.map(n => [ n.id, { ...n, description: [] } ]))
));
const result = projects.map(function(n) {
return {
...n,
description: this[n.id] ?? [],
};
}, runs.reduce((acc, n) => ((acc[n.projectId] = acc[n.projectId] ?? []).push(n), acc), {}));
const result = projects.map(n => ({
...n,
description: runs.filter(m => m.projectId === n.id),
}));
users.sort((a, b) => (a.age ?? Infinity) - (b.age ?? Infinity));
const sortedUsers = Object
.values(users.reduce((acc, n) => ((acc[n.age] = acc[n.age] ?? []).push(n), acc), {}))
.flat();
// или
const sorted = (arr, key) => arr
.map(n => [ n, key(n) ])
.sort((a, b) => a[1] - b[1])
.map(n => n[0]);
const sortedUsers = sorted(users, n => n.age ?? Infinity);
const unique = Object.values(arr.reduce((acc, n) => (acc[n.name] = n, acc), {}));
const names = unique.map(({ name }, i) => ({ id: -~i, name }));
const ages = unique.map((n, i) => ({ id: i + 1, age: n.age }));
const extractUnique = (arr, idKey, keys) =>
arr.reduce((acc, n) => {
if (!acc[1].has(n[idKey])) {
const id = acc[1].add(n[idKey]).size;
keys.forEach((k, i) => acc[0][i].push({ id, [k]: n[k] }));
}
return acc;
}, [ keys.map(() => []), new Set ])[0];
const [ names, ages ] = extractUnique(arr, 'name', [ 'name', 'age' ]);
<select id="city"></select>
<select id="warehouse"></select>
const citySelect = document.querySelector('#city');
const warehouseSelect = document.querySelector('#warehouse');
citySelect.addEventListener('change', function() {
updateSelectOptions(warehouseSelect, {
calledMethod: 'getWarehouses',
methodProperties: {
CityName: this.value,
},
});
});
updateSelectOptions(citySelect, {
calledMethod: 'getCities',
methodProperties: {
FindByString: '',
},
});
function updateSelectOptions(selectEl, queryData) {
fetch('https://api.novaposhta.ua/v2.0/json/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
modelName: 'Address',
apiKey: '',
...queryData,
}),
})
.then(r => r.json())
.then(r => selectEl.innerHTML = r.data
.map(n => `<option>${n.Description}</option>`)
.join('')
);
}
const includes = (a, b) => Object.entries(b).every(([ k, v ]) => Object.is(v, a[k]));
const check = (a, b) => Object.values(a).some(n => includes(n, b));
const result = check(parent, { a: '1', b: '1' });
const wrapper = document.querySelector('.block');
const blockSelector = '.block_one';
const activeClass = 'active';
wrapper.addEventListener('mouseover', onHover);
wrapper.addEventListener('mouseout', onHover);
function onHover({ type, target }) {
const block = type === 'mouseover' && target.closest(blockSelector);
this.querySelectorAll(blockSelector).forEach(n => {
n.classList.toggle(activeClass, !!block && n !== block);
});
}
.block:has(.block_one:hover) .block_one:not(:hover) {
/* сюда переносим стили того класса, который через js добавлялся */
}
document.querySelectorAll('.number').forEach(n => {
n.innerText = n.innerText.replace(/\d+/g, m => (+m).toLocaleString());
});
for (const n of document.getElementsByClassName('number')) {
n.textContent = n.textContent.replace(/\d(?=(\d{3})+(\D|$))/g, '$& ');
}
fetch('https://api.novaposhta.ua/v2.0/json/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
modelName: 'Address',
calledMethod: 'getWarehouses',
methodProperties: {
CityName: document.querySelector('.myCityName').textContent,
},
apiKey: '',
}),
})
.then(r => r.json())
.then(r => {
document.querySelector('.mySelect').innerHTML = r.data
.map(n => `<option>${n.Description}</option>`)
.join('');
});
setInterval(tesla.moveRight,30)
setInterval(tesla.moveRight.bind(tesla), 30)
setInterval(() => tesla.moveRight(), 30)
class Car {
...
moveRight = () => {
...
}
}
используется для получения и установки инлайновых стилей
const result = arr
.reduce((acc, n, i, a) => (
a[i - 1] !== n && acc.push([ n, 0 ]),
acc[acc.length - 1][1]++,
acc
), [])
.map(([ v, c ]) => c === 1 ? v : Array(c).fill(v));
const result = arr
.reduce((acc, n, i, a) => (
a[i - 1] === n || acc.push([]),
acc[acc.length - 1].push(n),
acc
), [])
.map(n => n.length === 1 ? n[0] : n);
const result = arr.reduce((acc, n, i, a) => {
const prev = n === a[i - 1];
const next = n === a[i + 1];
!prev && next && acc.push([]);
(prev || next ? acc[acc.length - 1] : acc).push(n);
return acc;
}, []);
Array.from({ length: 48 }, (n, i) => {
const d = new Date(0, 0, 0, 0, 30 * i);
return d.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' });
})