猿问

请教各位一个问题,求解答:怎么让对象中的对象的值相加各位有什么建议?

vardata={
"67407685":{
"24888861":200,
"23675650":300
},
"24888861":{
"23675650":789,
"23127112":400
},
"23675650":{
"24888861":789,
"67407685":900
},
"23127112":{
"5388329":340,
"24888861":123
},
"38121665":{
"24888861":400,
"23675650":123
},
"5388329":{
"24888861":789,
"23127112":400
}
}
我尝试循环,再循环,然后就懵了,求指点思路感激不尽
慕勒3428872
浏览 236回答 2
2回答

暮色呼如

我也不知道你要怎么相加。反正我是把data的每个属性的值分别相加了。vardata={"67407685":{"24888861":200,"23675650":300},"24888861":{"23675650":789,"23127112":400},"23675650":{"24888861":789,"67407685":900},"23127112":{"5388329":340,"24888861":123},"38121665":{"24888861":400,"23675650":123},"5388329":{"24888861":789,"23127112":400}};varadd=function(root_obj){for(letsecond_objinroot_obj){letsum=0;for(letthird_objinroot_obj[second_obj]){sum+=root_obj[second_obj][third_obj];}console.log(`${second_obj}-->${sum}`);}};add(data);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答