我已经挣扎了几个小时了。这就是我想要做的。
我有一组对象:
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}
]有些对象具有相同的id,我想将它们合并在一起。
time值进行求和。download: false,则合并对象应具有false,否则为true。到目前为止(我甚至还没有开始考虑下载键):
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
});我喜欢这样的输入或提示:)
发布于 2019-07-16 14:22:06
通过对象的地图将数组还原为id,然后使用Map.values()和Array.from() (或扩展到数组)将数组转换回数组。
注意:在这种情况下,使用Map作为累加器比使用对象更好,因为id属性是数字的。在ES6中,对象通常保持插入顺序,除非属性是数字的。当具有数字属性键的对象被转换为数组时,首先是数字属性,并根据它们的值排序。另一方面,Map总是保持插入的顺序。
const 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}
]
const result = Array.from(initArr.reduce((r, o) => {
if(!r.has(o.id)) r.set(o.id, { ...o });
else {
const current = r.get(o.id);
current.time += o.time;
current.download = current.download && o.download
}
return r;
}, new Map).values());
console.log(result);
发布于 2019-07-16 14:24:08
您可以reduce数组。创建一个累加器对象,每个id作为键,合并的对象作为值。如果id已经存在,使用当前对象更新time和download。否则,添加当前id作为键,并将当前对象的副本设置为它的值。然后使用Object.values()从这个累加器对象获取一个值数组。
const 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 }
]
const merged = initArr.reduce((acc, o) => {
if (acc[o.id]) {
acc[o.id].time += o.time;
acc[o.id].download = acc[o.id].download && o.download;
} else
acc[o.id] = { ...o };
return acc;
}, {})
console.log(Object.values(merged))
发布于 2019-07-16 14:39:04
试试这个,也支持下载选项。
const arr = [
{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}
]
const result = arr.reduce((res, obj) => {
const found = res.find(t => t.id === obj.id);
if(found){
found.time += obj.time;
found.download = found.download && obj.download;
} else {
res.push(obj);
}
return res;
}, []);
console.log(result);https://stackoverflow.com/questions/57059409
复制相似问题