javascript - Javascript:从更新对象数组更新嵌套对象值
问题描述
我有一个嵌套对象,没有可预先确定的子对象路径 - 例如:
{
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 函数生成更新列表 - 示例结果:
[{
content: "New Co",
id: "0064H00000ysL",
note: "Here's your update"
}, {
content: "91%",
id: "a0H4H00000Roi",
note: "New note here"
}]
我需要遍历更新对象数组并更新嵌套对象值,我尝试了一些东西,但似乎无法完全确定(我的 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 函数工作正常,但是当我尝试循环更新数组并写回主对象 (obj) 时,我得到Uncaught TypeError: Cannot read properties of undefined (reading 'id')" 。
任何帮助表示赞赏
解决方案
您可以在这里使用递归方法,我们将创建一个函数updateNestedObj()
来应用更新,应用于每个对象及其任何子对象:
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; }
推荐阅读
- swift - 如何防止后退按钮消失?
- spring - 将单模块 gradle 项目转换为多模块
- javascript - 在javascript中替换深层嵌套在对象中的函数
- r - 你能用 rOxygen2 分享函数之间的例子吗?
- java - id always = 0 因为结果无法更新数据库中的记录
- reactjs - Intersection Observer 丢失条目
- flutter - 如何在颤动的抽屉中添加分隔符和字幕
- typescript - 当我无头运行时我的测试失败了,因为它是不可见的
- javascript - 模态打开时显示相同的视频
- python - 我不知道为什么这个 for 循环程序不起作用(python)。解决方案?