function uniqueWithSum(arr, idKey, sumKey) {
const unique = {};
for (const n of arr) {
const id = n[idKey];
if (!unique.hasOwnProperty(id)) {
unique[id] = { ...n, [sumKey]: 0 };
}
unique[id][sumKey] += n[sumKey];
}
return Object.values(unique);
}
const result = uniqueWithSum(arr, 'id', 'duration');
UPD. Сократим и осовременим код; кроме того, обеспечим корректную обработку массивов, элементами которых также являются массивы - вместо объектов будем создавать экземпляры того же класса, что и элементы исходного массива:
const uniqueWithSum = (arr, idKey, sumKey) =>
Object.values(arr.reduce((acc, n) => (
acc[n[idKey]] ??= Object.assign(new n.constructor, n, { [sumKey]: 0 }),
acc[n[idKey]][sumKey] += n[sumKey],
acc
), {}));