So i've been struggling for a few hours now. Here's what I'm trying to do.
I have an array of objects:
initArr = [
{id: 5, time: 100, download: true},
{id: 2, time: 50, download: false},
{id: 3, time: 1000, download: true},
{id: 5, time: 50, download: true},
{id: 5, time: 550, download: false},
{id: 2, time: 1500, download: true}
]
some objects have the same id
, I want to merge them together.
- but when merging i want to sum the time
value of both merged object .
- if one of the two merged objects has download: false
, the merged object should have false
, otherwise true
.
Here's what I have so far (I did not start yet even considering the download key):
const mergedArr= [];
initArr.map(obj => obj['id'])
.map((id, i, arr) => {
if (arr.indexOf(id) === i) {
mergedArr.push(initArr[i]);
} else {
const mergeIndex = mergedArr.map( x => x.id).indexOf(id);
mergedArr[mergeIndex].playTime +=initArr[arr.indexOf(id)].playTime;
}
return mergedArr
});
I'd love so inputs or hints :)