标签:return turn function div length script color val each
var data = [ { province: 1, index:1}, { province: 2, index:3}, { province: 1, index:3}, { province: 2, index:2} ]
想要得到的格式
{ province: 1, index:4}, { province: 2, index:5}
实现过程
function mergeArr(arr){ var newArr=[]; arr.forEach(item=>{ var dataItem =item if(newArr.length>0){ var filterValue = newArr.filter(v=>{ return v.province == dataItem.province }) if(filterValue.length>0){ newArr.forEach(n=>{ if( n.province ==filterValue[0].province){ n.index = Number(filterValue[0].index) + Number(dataItem.index) } }) }else{ newArr.push(dataItem) } }else{ newArr.push(dataItem) } }) return newArr }
调用
mergeArr(data)
标签:return turn function div length script color val each
原文地址:https://www.cnblogs.com/xhrr/p/11990900.html