白猪掌柜的
我不确定这是否是你想要的:var obj={"Myanmar": [{ "trk1": { "mou": 0.0, "ca": 1.0, "ccs": 0.0, "ccc": 0.0 }}],"Gibraltar": [{ "trk1": { "mou": 12.850000381469727, "ca": 1.0, "ccs": 1.0, "ccc": 3.0 }}],"Cyprus": [{ "trk1": { "mou": 36.25, "ca": 3.0, "ccs": 2.0, "ccc": 7.0 }, "trk2": { "mou": 27.299999237060547, "ca": 1.0, "ccs": 1.0, "ccc": 6.0 }, "trk3": { "mou": 80.59999752044678, "ca": 4.0, "ccs": 4.0, "ccc": 14.0 }, "trk4": { "mou": 26.716670513153076, "ca": 4.0, "ccs": 4.0, "ccc": 6.0 }}]}result = Object.entries(obj).reduce((acc, [k,v])=>{ acc[k] = acc[k] || {}; Object.entries(v[0]).forEach(([_, country])=>{ for(let [key, value] of Object.entries(country)){ acc[k][key] = acc[k][key] || 0; acc[k][key] += value; } }) return acc;},{});console.log(result);
长风秋雁
var obj={"Myanmar": [{ "trk1": { "mou": 0.0, "ca": 1.0, "ccs": 0.0, "ccc": 0.0 }}],"Gibraltar": [{ "trk1": { "mou": 12.850000381469727, "ca": 1.0, "ccs": 1.0, "ccc": 3.0 }}],"Cyprus": [{ "trk1": { "mou": 36.25, "ca": 3.0, "ccs": 2.0, "ccc": 7.0 }, "trk2": { "mou": 27.299999237060547, "ca": 1.0, "ccs": 1.0, "ccc": 6.0 }, "trk3": { "mou": 80.59999752044678, "ca": 4.0, "ccs": 4.0, "ccc": 14.0 }, "trk4": { "mou": 26.716670513153076, "ca": 4.0, "ccs": 4.0, "ccc": 6.0 }}]}let reqObj={};let arr = Object.keys(obj);for(var i=0;i<=arr.length-1;i++){ let inObj = obj[arr[i]][0]; reqObj[arr[i]] = Object.keys(inObj).reduce((acc,ele,index)=>{ let keyArr = Object.keys(inObj[ele]); for(var j=0;j<=keyArr.length-1;j++){ acc = {...acc,...{[keyArr[j]] : (keyArr[j] in acc) ? acc[keyArr[j]] + inObj[ele][keyArr[j]] : inObj[ele][keyArr[j]]}} } return acc; },{})} console.log(reqObj)