//原始数据数组
var res=[{name:“test1”,num:1},{name:“test2”,num:7},{name:“test1”,num:4},{name:“test2”,num:5}];
//定义map对象
var map={}
//遍历聚合原始数据
for(var i=0;i<res.length;i++){
var oldData = res[i];
var data = {};
if{map[oldData.name]==null}{
data.name = res[i].name;
data.num = res[i].num;
map[oldData.name] =data
}else{
data = map[oldData.name]
data.num +=olddData.num;
}
}
//组装新数组
var relDatas =[]
for(var i in map){
relDatas.push(map[i]);
}
js 利用数组,对象 对数据进行简单聚合
最新推荐文章于 2024-01-12 13:46:13 发布