Javascript 计算嵌套json的子级中的值,并将其作为新元素添加到原始嵌套json
我有以下建议:Javascript 计算嵌套json的子级中的值,并将其作为新元素添加到原始嵌套json,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我有以下建议: click: false image: "xxxx" items: Array(4) 0: click: false image: "xxxx/chapter.png" items: Array(12) 0: click: true courseID: 1 coverImage:"xxxxx" duration: "(2':20)" image
click: false
image: "xxxx"
items: Array(4)
0:
click: false
image: "xxxx/chapter.png"
items: Array(12)
0:
click: true
courseID: 1
coverImage:"xxxxx"
duration: "(2':20)"
image: "xxxx/chapter.png"
pdfLinkIos:"xxxxx"
title: "Motion title"
videoLink: "xxxx"
_internalId: "MV6ZgJ_BxxeD"
我想添加一个用异步函数计算的元素。应针对具有“单击:真”的标高执行计算。我可以成功地解析函数,甚至可以正确地计算它。
这是我的密码:
parseValues=(obj)=> {
for(var k in obj) {
if(obj[k] instanceof Object) {
this.parseValues(obj[k]);
} else {
console.log("[coverRnder] all", k, );
if(obj["click"]){
return this.accumulateRating(obj).then(rating => {
obj.push(rate: rating);
});
//obj[k].push("");
}
}
}
};
async accumulateRating(node) {
return;
}
我唯一的问题是,我不知道如何将计算出的元素添加到原始json的适当级别。(this.state.courseContent)。
事实上,我需要的是在我的原始json中有累加的输出,速率标题如下:
click: false
image: "xxxx"
items: Array(4)
0:
click: false
image: "xxx"
items: Array(12)
0:
click: true
courseID: 1
coverImage:"xxxxxx"
duration: "(2':20)"
image: "xxxx"
pdfLinkIos:"xxxxxx"
title: "Motion title"
videoLink: "xxxx"
_internalId: "MV6ZgJ_BxxeD"
rate: 3.5
你能帮帮我吗?push()
用于数组而不是对象。要向对象添加键/值对,只需定义它即可
return this.accumulateRating(obj).then(rating => {
obj.rate = rating;
//obj["rate"] = rating; also works
});
您不是发布对象,而是发布控制台转储。也许可以使用[]
代码段编辑器创建@mplungjan:谢谢您的回答。我累了,想挤进去,但我做不到。我让它最小化