Javascript ES6计算对象数组的总和
我有多个对象数组,它们具有字符串属性Javascript ES6计算对象数组的总和,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,我有多个对象数组,它们具有字符串属性“CountType”和数字属性“ItemCount” 必须将这些对象相加,才能按“CountType”属性计算所有项目的总数 例如,我需要知道卷、页、照片等的总数 尝试#1:我尝试的这段代码返回0。这可能是因为我需要遍历外部数组来跨嵌套数组进行比较 function sumByProperty(items, prop) { if (items == null) { return 0; } return items.reduce(fun
“CountType”
和数字属性“ItemCount”
必须将这些对象相加,才能按“CountType”
属性计算所有项目的总数
例如,我需要知道卷、页、照片等的总数
尝试#1:我尝试的这段代码返回0。这可能是因为我需要遍历外部数组来跨嵌套数组进行比较
function sumByProperty(items, prop) {
if (items == null) {
return 0;
}
return items.reduce(function (a, b) {
return b[prop] == null ? a : a + b[prop];
}, 0);
}
输入
项目计数
[
[
{"CountType":"Volumes","ItemCount":3},
{"CountType":"Sheets","ItemCount":6},
{"CountType":"Photos","ItemCount":3},
{"CountType":"Boxes","ItemCount":1},
{"CountType":"Other","ItemCount":2}
],
[
{"CountType":"Volumes","ItemCount":1},
{"CountType":"Sheets","ItemCount":1},
{"CountType":"Photos","ItemCount":3},
{"CountType":"Boxes","ItemCount":0},
{"CountType":"Other","ItemCount":1}
],
[
{"CountType":"Volumes","ItemCount":1},
{"CountType":"Sheets","ItemCount":0},
{"CountType":"Photos","ItemCount":3},
{"CountType":"Boxes","ItemCount":4},
{"CountType":"Other","ItemCount":5}
]
]
去离子输出
总数
[
{"CountType":"Volumes","ItemCount":5},
{"CountType":"Sheets","ItemCount":7},
{"CountType":"Photos","ItemCount":9},
{"CountType":"Boxes","ItemCount":5},
{"CountType":"Other","ItemCount":8}
]
更新:以下是我尝试运行函数的方式:
这可能是因为我需要遍历外部数组来跨嵌套数组进行比较
function sumByProperty(items, prop) {
if (items == null) {
return 0;
}
return items.reduce(function (a, b) {
return b[prop] == null ? a : a + b[prop];
}, 0);
}
实际上,您可以将阵列展平:
function sumByProperty(items, prop) {
return items.flat().reduce(function (a, b) {
return b[prop] == null ? a : a + b[prop];
}, 0);
}
const result = [
{ CountType: "Volumes", ItemCount: sumByProperty(input, "Volumes"), },
//...
];
但我宁愿使用哈希表动态分组,这样您只需迭代一次,而不需要命名所有属性:
const hash = new Map();
for(const { CountType, ItemCount } of input.flat())
hash.set(CountType, (hash.get(CountType) || 0) + ItemCount);
const result = [...hash.entries()].map(([CountType, ItemCount]) => ({ CountType, ItemCount }));
Try(数据是输入的,h={},r是结果)
let数据=[[
{“CountType”:“Volumes”,“ItemCount”:3},
{“CountType”:“Sheets”,“ItemCount”:6},
{“CountType”:“Photos”,“ItemCount”:3},
{“CountType”:“box”,“ItemCount”:1},
{“CountType”:“其他”,“ItemCount”:2}
],
[
{“CountType”:“Volumes”,“ItemCount”:1},
{“CountType”:“Sheets”,“ItemCount”:1},
{“CountType”:“Photos”,“ItemCount”:3},
{“CountType”:“Box”,“ItemCount”:0},
{“CountType”:“其他”,“ItemCount”:1}
],
[
{“CountType”:“Volumes”,“ItemCount”:1},
{“CountType”:“Sheets”,“ItemCount”:0},
{“CountType”:“Photos”,“ItemCount”:3},
{“CountType”:“box”,“ItemCount”:4},
{“CountType”:“其他”,“ItemCount”:5}
]];
设r,h={};
data.map(a=>a.map(x=>h[x.CountType]=x.ItemCount+(h[x.CountType]| | 0)))
r=Object.keys(h).map(k=>({CountType:k,ItemCount:h[k]}));
控制台日志(r)代码>这里是一个使用2个reduce函数和一个的示例,用于。。。在…
中,将结果合并回父减速器
诀窍是确保将累加器初始化为null,以便定义自己的累加器。然后,只需减少数组的数量,并使用另一个减缩器来运行对象数组。完成后,只需将内部数组的结果合并回父数组。(这可能是更好的方法)
小提琴:
const json=[
[
{“CountType”:“Volumes”,“ItemCount”:3},
{“CountType”:“Sheets”,“ItemCount”:6},
{“CountType”:“Photos”,“ItemCount”:3},
{“CountType”:“box”,“ItemCount”:1},
{“CountType”:“其他”,“ItemCount”:2}
],
[
{“CountType”:“Volumes”,“ItemCount”:1},
{“CountType”:“Sheets”,“ItemCount”:1},
{“CountType”:“Photos”,“ItemCount”:3},
{“CountType”:“Box”,“ItemCount”:0},
{“CountType”:“其他”,“ItemCount”:1}
],
[
{“CountType”:“Volumes”,“ItemCount”:1},
{“CountType”:“Sheets”,“ItemCount”:0},
{“CountType”:“Photos”,“ItemCount”:3},
{“CountType”:“box”,“ItemCount”:4},
{“CountType”:“其他”,“ItemCount”:5}
]
];
const counts=json.reduce((acc,currentArray)=>{
acc=acc?acc:{};
const arrCounts=currentArray.reduce((_acc,_item)=>{
_acc=_acc?_acc:{};
_acc[\u item.CountType]=\u acc[\u item.CountType]?\u acc[\u item.CountType]+\u item.ItemCount:\u item.ItemCount;
返回acc;
},空);
用于(arrCounts中的常数项){
acc[项目]=acc[项目]?acc[项目]+arrCounts[项目]:arrCounts[项目];
}
返回acc;
},空);
控制台日志(计数)
您的sumByProperty
函数似乎很好。请告诉我们你是如何称呼它的输入这不是代码高尔夫。为什么h
,r
?这让事情变得不可理解为什么.map
?嘿-为什么map
?:)因为它保证了O(1)查找时间,因此是合适的数据结构。这并不能保证O(1)查找时间(尽管大多数引擎会选择哈希表),但我更喜欢Map的明确性。有趣的信息(+1)-任何证明(或证明链接)?MDN的Map
文档有一个部分值得一提。