gpt4 book ai didi

javascript - 在 JS 对象中通过 id 查找子对象并推送到它

转载 作者:行者123 更新时间:2023-12-01 02:29:42 24 4
gpt4 key购买 nike

我有一个像这样的对象,我想通过 id 找到一个子对象并向其推送一个新的子对象。

{
'id': '234567869',
'name': 'Lao Lao',
'title': 'general manager',
'children': [{
'id': '467876756634',
'name': 'Bo Miao',
'title': 'department manager'
}, {
'id': '2345666078',
'name': 'Su Miao',
'title': 'department manager',
'children': [{
'id': '898735342',
'name': 'Tie Hua',
'title': 'senior engineer'
}, {
'id': '7697347548',
'name': 'Hei Hei',
'title': 'senior engineer',
'children': [{
'id': '123415640',
'name': 'Pang Pang',
'title': 'engineer'
}, {
'id': '1237450976',
'name': 'Xiang Xiang',
'title': 'UE engineer'
}]
}]
}, {
'id': '6968756535',
'name': 'Yu Jie',
'title': 'department manager'
}, {
'id': '236448654',
'name': 'Chun Miao',
'title': 'department manager'
}, {
'id': '356898765',
'name': 'Yu Tie',
'title': 'department manager'
}]}

我想向 id 为“898735342”或“234567869”的对象添加一个子对象。

我使用了这个递归函数,但不起作用。这应该在我们的对象中搜索“id”并将“newObj”作为子对象推送给它。

var newObj = {
'id': '999999999',
'name': 'new name',
'title': 'new added child',
};

function findNodeAndUpdate(idToFind, bigObjectToSearch, newObj) {
var i, currentChild, result;
if (idToFind == bigObjectToSearch.id) {
bigObjectToSearch.children.push(newObj);
return true;
} else {
// Use a for loop instead of forEach to avoid nested functions
// Otherwise "return" will not work properly
for (i = 0; i < bigObjectToSearch.children.length; i += 1) {
currentChild = bigObjectToSearch.children[i];
// Search in the current child
result = findNodeAndUpdate(idToFind, currentChild, newObj);
// Return the result if the node has been found
if (result !== false) {
bigObjectToSearch.children.push(newObj);
return true;
}
}
// The node has not been found and we have no more options
return false;
}
}

另一个功能仍然无法工作:

function findObjectById(root, id) {
if (root.id == id){
return root;
}
if (root.children) {
for (var k in root.children) {
if (root.children[k].id == id) {
return root.children[k];
}
else if (root.children.length) {
return findObjectById(root.children[k], id);
}
}
}
};

最佳答案

请尝试以下代码:

var obj = {
'id': '234567869',
'name': 'Lao Lao',
'title': 'general manager',
'children': [{
'id': '467876756634',
'name': 'Bo Miao',
'title': 'department manager'
}, {
'id': '2345666078',
'name': 'Su Miao',
'title': 'department manager',
'children': [{
'id': '898735342',
'name': 'Tie Hua',
'title': 'senior engineer'
}, {
'id': '7697347548',
'name': 'Hei Hei',
'title': 'senior engineer',
'children': [{
'id': '123415640',
'name': 'Pang Pang',
'title': 'engineer'
}, {
'id': '1237450976',
'name': 'Xiang Xiang',
'title': 'UE engineer'
}]
}]
}, {
'id': '6968756535',
'name': 'Yu Jie',
'title': 'department manager'
}, {
'id': '236448654',
'name': 'Chun Miao',
'title': 'department manager'
}, {
'id': '356898765',
'name': 'Yu Tie',
'title': 'department manager'
}]
};

var newObj = {
'id': '999999999',
'name': 'new name',
'title': 'new added child',
};

function findNodeAndUpdate(idToFind, bigObjectToSearch, newObj) {
var i, currentChild, result;
if (idToFind == bigObjectToSearch.id) {
bigObjectToSearch.children = bigObjectToSearch.children || [];
bigObjectToSearch.children.push(newObj);
return true;
} else if (bigObjectToSearch.children) {
for (i = 0; i < bigObjectToSearch.children.length; i += 1) {
currentChild = bigObjectToSearch.children[i];
// Search in the current child
if(findNodeAndUpdate(idToFind, currentChild, newObj)){
break;
}
}
return false;
}
return false;
}

findNodeAndUpdate('898735342', obj, newObj);

console.log(obj);

要删除对象,

function deleteNode(idToFind, bigObjectToSearch) {
var i, currentChild, result;
if (idToFind == bigObjectToSearch.id) {
return true;
} else if (bigObjectToSearch.children) {
for (i = 0; i < bigObjectToSearch.children.length; i += 1) {
currentChild = bigObjectToSearch.children[i];
// Search in the current child
if(deleteNode(idToFind, currentChild)){
//delete bigObjectToSearch.children[i];
bigObjectToSearch.children.splice(i,1);
break;
};
}
return false;
}
return false;
}

希望有帮助:)

关于javascript - 在 JS 对象中通过 id 查找子对象并推送到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48400340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com