[英]Javascript: update nested object values from array of update objects
我有一個嵌套的 object,沒有可預先確定的子對象路徑 - 例如:
{
children :
[
{
children :
[
{
children :
[
{
id : "F100517B-D00F",
level : 4,
note : "update me",
parentId : "23A2A0DB-CCE3",
title : "change me"
}
],
id : "23A2A0DB-CCE3",
level : 3,
note : "me too",
parentId : "a0H4H00000Roi",
title : "..and me"
}
],
id : "a0H4H00000Roi",
level : 2,
note : "none",
parentId : "0064H00000ysL",
title : "pending"
},
{
"children" :
[
{
id : "6A45E2EC-7825",
level : 3,
note : "|",
parentId : "a0H4H00000Roi",
title : ""
}
],
id : "a0H4H00000Roi",
level : 2,
note : "",
parentId : "0064H00000ysL",
title : "Change me"
}
],
id : "0064H00000ysL",
level : 1,
note : "hello",
title : "Test Co"
}
在此之后,我通過 map function 生成更新列表 - 示例結果:
[{
content: "New Co",
id: "0064H00000ysL",
note: "Here's your update"
}, {
content: "91%",
id: "a0H4H00000Roi",
note: "New note here"
}]
我需要遍歷更新 object 數組並更新嵌套的 object 值,我嘗試了一些方法但似乎無法完全確定(我的 JS 技能有點受限 atm)。
這是我最后一次嘗試,取自我在這里找到的最接近的解決方案: Javascript update values in nested object by array path
var updates = $('div.node').map(function() {
return {
id: $(this).attr("id"),
content: $(this).find('div.content').text(),
note: $(this).find('div.note').text()
};
}).get();
const checkAndChange = (obj, update) => { //function to check id match for update
if (update.id.includes(obj.id)) {
obj.title = update.content;
obj.note = update.note;
}
}
const recursion = (obj, update) => {
const o = obj;
checkAndChange(o, update); // check if id exists update values
if (o.children.length > 0) { //check if has children
o.children.forEach(v => { //if has children do same recursion for children
recursion(v, update);
});
}
return o; //return updated object
}
var updatesLength = updates.length;
for(let it = 0; it < updatesLength; it++) {
recursion(obj, updates[it]);
}
console.log(obj)
頂部縮進的 map function 工作正常,但當我嘗試遍歷更新數組並寫回主 object (obj) 時,我得到了Uncaught TypeError: Cannot read properties of undefined (reading 'id')” 。
任何幫助表示贊賞
您可以在這里使用遞歸方法,我們將創建一個 function updateNestedObj()
來應用更新,應用於每個 object 及其任何子對象:
const objToUpdate = { children: [ { children: [ { children: [ { id: "F100517B-D00F", level: 4, note: "update me", parentId: "23A2A0DB-CCE3", title: "change me" } ], id: "23A2A0DB-CCE3", level: 3, note: "me too", parentId: "a0H4H00000Roi", title: "..and me" } ], id: "a0H4H00000Roi", level: 2, note: "none", parentId: "0064H00000ysL", title: "pending" }, { "children": [ { id: "6A45E2EC-7825", level: 3, note: "|", parentId: "a0H4H00000Roi", title: "" } ], id: "a0H4H00000Roi", level: 2, note: "", parentId: "0064H00000ysL", title: "Change me" } ], id: "0064H00000ysL", level: 1, note: "hello", title: "Test Co" } const updateArr = [{ content: "New Co", id: "0064H00000ysL", note: "Here's your update" }, { content: "91%", id: "a0H4H00000Roi", note: "New note here" }]; function updateNestedObj(obj, updates) { const updateToApply = updates.find(upd => upd.id === obj.id); if (updateToApply) { obj.title = updateToApply.content; obj.note = updateToApply.note; } // Apply updates to any child objects for(let k in obj) { if (typeof(obj[k]) === 'object') { updateNestedObj(obj[k], updates); } } } updateNestedObj(objToUpdate, updateArr); console.log(objToUpdate)
.as-console-wrapper { max-height: 100%;important: top; 0; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.