JSON数组如下
[{"id":"26","buName":"CAS","totFocCust":23,"totCustResp":10,"totFocProj":160,"totProjResp":13,"wtProjScore":1076.65,"teamSize":246},{"id":"33","buName":"ECO","totFocCust":0,"totCustResp":0,"totFocProj":0,"totProjResp":0,"wtProjScore":0,"teamSize":0},{"id":"28","buName":"DAS","totFocCust":0,"totCustResp":0,"totFocProj":0,"totProjResp":0,"wtProjScore":0,"teamSize":0},{"id":"27","buName":"DCS","totFocCust":9,"totCustResp":0,"totFocProj":31,"totProjResp":0,"wtProjScore":0,"teamSize":0},{"id":"29","buName":"EAS","totFocCust":24,"totCustResp":4,"totFocProj":114,"totProjResp":5,"wtProjScore":434.83,"teamSize":89},{"id":"30","buName":"IMS","totFocCust":9,"totCustResp":0,"totFocProj":25,"totProjResp":0,"wtProjScore":0,"teamSize":0}]
我需要添加wtProjScore和teamSize的值,其中buName是CAS,EAS和DAS,并将它与其他buName一起显示在同一个数组中,例如。如下所示。使用angularJS迭代JSON数组。
[{"id":"2689","buName":"ABS","totFocCust":23,"totCustResp":10,"totFocProj":160,"totProjResp":13,"wtProjScore":1511.48,"teamSize":335},{"id":"33","buName":"CISCO","totFocCust":0,"totCustResp":0,"totFocProj":0,"totProjResp":0,"wtProjScore":0,"teamSize":0},{"id":"27","buName":"DCS","totFocCust":9,"totCustResp":0,"totFocProj":31,"totProjResp":0,"wtProjScore":0,"teamSize":0},{"id":"30","buName":"IMS","totFocCust":9,"totCustResp":0,"totFocProj":25,"totProjResp":0,"wtProjScore":0,"teamSize":0}]
答案 0 :(得分:0)
您可以使用JavaScript filter
和reduce
来完成此任务:
a = [{"id":"26","buName":"CAS","totFocCust":23,"totCustResp":10,"totFocProj":160,"totProjResp":13,"wtProjScore":1076.65,"teamSize":246},{"id":"33","buName":"ECO","totFocCust":0,"totCustResp":0,"totFocProj":0,"totProjResp":0,"wtProjScore":0,"teamSize":0},{"id":"28","buName":"DAS","totFocCust":0,"totCustResp":0,"totFocProj":0,"totProjResp":0,"wtProjScore":0,"teamSize":0},{"id":"27","buName":"DCS","totFocCust":9,"totCustResp":0,"totFocProj":31,"totProjResp":0,"wtProjScore":0,"teamSize":0},{"id":"29","buName":"EAS","totFocCust":24,"totCustResp":4,"totFocProj":114,"totProjResp":5,"wtProjScore":434.83,"teamSize":89},{"id":"30","buName":"IMS","totFocCust":9,"totCustResp":0,"totFocProj":25,"totProjResp":0,"wtProjScore":0,"teamSize":0}]
function check(x){
return x.buName == "CAS" || x.buName == "EAS" || x.buName == "DAS";
}
b = a.filter((x)=>{return check(x)})
c = b.reduce((x,y)=>{return x+y.wtProjScore},0)
d = b.reduce((x,y)=>{return x+y.teamSize},0)
console.log("For CAS, EAS, DAS:")
console.log("Total wtProjScore: ",c)
console.log("Total teamSize: ",d)
如果您希望以不同方式呈现您的要求,请说明您的要求。或者您可以根据需要自行调整此代码
答案 1 :(得分:0)
您可以使用简单的 var totprojScore = totTeamSize = 0;
$scope.newJSONArray = []; //Use this to bind your view.
yourJSONArray.forEach(function(obj) {
if(obj.buName == "CAS" || obj.buName == "EAS" || obj.buName == "DAS"){
totprojScore += obj.wtProjScore ;
totTeamSize += obj.teamSize;
}
else{
$scope.newJSONArray.push(obj);
}
});
$scope.newJSONArray.push({
"id":temp,
"buName":"tempName",
"wtProjScore":totprojScore ,
"teamSize":totTeamSize
});
循环来实现此目的。
{{1}}