为了账号安全,请及时绑定邮箱和手机立即绑定

遍历json,求合计?

/ 猿问

遍历json,求合计?

Zoe_z 2019-06-11 22:46:59

代码如下:

[{"CountAll":30,"Zone":"A","WareHouse":"原料库"},{"CountAll":20,"Zone":"B","WareHouse":"成品库"},{"CountAll":60,"Zone":"C","WareHouse":"原料库"},{"CountAll":70,"Zone":"D","WareHouse":"成品库"},{"CountAll":20,"Zone":"F","WareHouse":"食品库"},{"CountAll":10,"Zone":"E","WareHouse":"储存库"}]

循环统计出:

不同的WareHouse(仓库),CountAll 的总计 

查看完整描述

1 回答

?
pardon110

用个for...of即可,代码如下

var res = {}
for(const v of tj){
   if (!res[v["WareHouse"]]){
       res[v["WareHouse"]] = v["CountAll"]
}else{
       res[v["WareHouse"]] += v["CountAll"]
}
}
console.log(res)
// { '原料库': 90, '成品库': 90, '食品库': 20, '储存库': 10 }


查看完整回答
反对 回复 2019-06-12

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信