【发布时间】:2020-02-16 18:54:49
【问题描述】:
所以我有了下一个信息,我想要实现的是过滤对象数组并将重复对象的数量相加
//Original Data
var data = {
sublists = [
{ item: 1, qty: 2},
{ item: 2, qty: 2},
{ item: 3, qty: 2},
{ item: 1, qty: 5}
{ item: 3, qty: 3}
],
...
};
//This is what I want to achieve
var result = {
sublists = [
{ item: 1, qty: 7},
{ item: 2, qty: 2},
{ item: 3, qty: 5}
],
...
};
到目前为止,我得到的是重复的项目名称和具有此变量数据的对象
var repeatedObjects = {
1: [{ item: 1, qty: 2}, { item: 1, qty: 5}],
3: [{ item: 3, qty: 2}, { item: 3, qty: 3}]
}
var repeatedItems = [1, 3];
但我坚持减少对象,这样我就可以得到原始数据,只有一个重复的对象和所有对象的总和。有什么想法吗?
【问题讨论】:
标签: javascript arrays sorting