const uniqueWithSum = (arr, idKey, sumKey) =>
Object.values(arr.reduce((acc, n) => {
const id = n[idKey];
acc[id] = acc[id] || Object.assign(new n.constructor, n, { [sumKey]: 0 });
acc[id][sumKey] += n[sumKey];
return acc;
}, {}));
// ваш случай
const result = uniqueWithSum(arr, 'id', 'duration');
// элементам не обязательно быть объектами, это могут быть и массивы
uniqueWithSum([
[ 'A', 1 ],
[ 'B', 5 ],
[ 'A', 2 ],
[ 'A', 3 ],
], 0, 1) // [ [ 'A', 6 ], [ 'B', 5 ] ]
const capitalize = str => str.replace(/(^|\s|-)+\S/g, m => m.toUpperCase());
$input.on('input', function() {
this.value = Math.min(MAX_VALUE, Math.max(MIN_VALUE, this.value));
});
const chainScripts = src => src.reduce((acc, n) => {
return acc.then(() => new Promise((resolve, reject) => {
const s = document.createElement('script');
s.onload = resolve;
s.onerror = reject;
s.src = n;
document.head.appendChild(s);
}));
}, Promise.resolve());
const getNested = (root, path) => path.split('.').reduce((p, c) => p != null ? p[c] : p, root);
const id = getNested(e, 'path.1.attributes.uid.textContent');
// можно немного сократить функцию извлечения вложенных значений
const getNested = (root, path) => path.split('.').reduce((p, c) => p?.[c], root);
// а можно и вообще отказаться от этой функции, смысла в ней теперь немного
const id = e?.path?.[1]?.attributes?.uid?.textContent;
function handler(e) {
console.log('hello, world!!');
this.removeEventListener(e.type, handler);
}
document.addEventListener('click', handler);
document.addEventListener('click', handler, { once: true });
const result = arr.reduce((acc, n, i) => {
if (здесь вы проверяете элемент массива на соответствие своему условию) {
let group = acc[acc.length - 1];
if (!group || group.index !== i - 1) {
group = { data: [] };
acc.push(group);
}
group.index = i;
group.data.push(n);
}
return acc;
}, []).map(n => n.data);
phone.value = phone.value.replace(/[^+0-9]/g, '').slice(0, 11);
const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext('2d');
const TILE_SIDE = 32;
let pickX = 0;
let pickY = 0;
const ground = new Image();
ground.src = 'Ground.png';
const pick = new Image();
pick.src = 'Pick.png';
document.addEventListener('keydown', function(e) {
switch (e.keyCode) {
case 87: pickY -= TILE_SIDE; break;
case 65: pickX -= TILE_SIDE; break;
case 83: pickY += TILE_SIDE; break;
case 68: pickX += TILE_SIDE; break;
default: return;
}
draw();
});
function draw() {
for (let x = 0; x < canvas.width; x += TILE_SIDE) {
for (let y = 0; y < canvas.height; y += TILE_SIDE) {
ctx.drawImage(ground, x, y);
}
}
ctx.drawImage(pick, pickX, pickY);
}
draw();
<div class="accordionFaq"></div>
const playersOptions = [
{ source: '...' },
{ source: '...' },
...
];
$('.accordionFaq').html(playersOptions.map((n, i) => `
<div class="accordionFaq-item">
<div class="accordionFaq-head js-accordionFaq-head">
Video #${i}
</div>
<div class="accordionFaq-body">
<div id="player${i}"></div>
</div>
</div>
`).join('')).on('click', '.accordionFaq-head', function(e) {
const index = $(this).closest('.accordionFaq-item').index();
$('.accordionFaq-item', e.delegateTarget).each(function(i) {
const
$this = $(this),
isClicked = i === index,
active = isClicked && !$this.hasClass('active');
$(this)
.toggleClass('active', active)
.find('.accordionFaq-body')
[isClicked ? 'slideToggle' : 'slideUp']();
playersOptions[i].player[active ? 'play' : 'pause']();
});
});
playersOptions.forEach((n, i) => {
n.player = new Clappr.Player({
source: n.source,
parentId: `#player${i}`,
});
});