猿问
下载APP

遍历json,求合计?

代码如下:

[{"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 的总计 

Zoe_z
浏览 114回答 1
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 }
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答