1 回答
TA贡献1777条经验 获得超3个赞
你可以这样做
遍历数组
检查属性名称是否等于“repl”对象中的键
如果是,请使用“repl”对象中的值重新分配该值
let stru = {
"class": "List",
"list": [{
"name": "HandsUp",
"schedule": {
"type": "probability",
"theme": "Regular",
"occurance": {
"next": 1607687249008.9834,
"prev": null
}
}
}, {
"name": "Listing",
"waitingScreenInfo": {
"__class": "WaitingScreenInfo",
"getRecapTime": 1607687753.7949834
},
"schedule": {
"type": "Waiting2",
"theme": "Listing",
"occurance": {
"next": 1607687249008.9834,
"prev": null
}
}
}]
}
const repl = {
"HandsUp": "HandsDown",
"Listing": "ImgList",
"Waiting2": "UpNDown"
}
console.log("Before ", stru)
stru.list.forEach(element => {
// keys and values correspond at the index
let keys = Object.keys(repl);
let values = Object.values(repl);
for (let i = 0; i < keys.length; i++) {
if (keys[i] === element.name) {
element.name = values[i];
}
}
});
console.log("Afterwards ", stru)
添加回答
举报