提问人:panshul yogesh 提问时间:7/14/2023 最后编辑:Alexander Nenashevpanshul yogesh 更新时间:7/14/2023 访问量:44
如何在嵌套数组中找到对象的键值对之前添加新对象
How to add a new object before found object's key value pair in nested array
问:
我尝试过的代码,但数组中没有变化
const data = [
{
name: 'Fruit',
children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
},
{
name: 'Vegetables',
children: [
{
name: 'Green',
children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
},
{
name: 'Orange',
children: [{ name: 'Pumpkins' }, { name: 'Carrots' }],
},
],
},
];
所需输出
const data = [
{
name: 'Fruit',
children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
},
{
name: 'Vegetables',
children: [
{
name: 'Green',
children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
},
{
name: 'Orange',
children: [{ name: 'Pumpkins' },{name:'newname'} { name: 'Carrots' }],
},
\],
},
\];
function addObjectBefore(arr, targetName, newObj) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].children) {
addObjectBefore(arr[i].children, targetName, newObj); // Recursive call for nested children array
}
if (arr[i].name === targetName) {
const targetIndex = arr[i]?.children?.findIndex(obj => obj.name === targetName);
arr[i]?.children?.splice(targetIndex, 0, newObj); // Insert new object before the target
break; // Stop the iteration once the target is found and object is inserted
}
}
}
// Given data
const data = [
{
name: 'Fruit',
children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
},
{
name: 'Vegetables',
children: [
{
name: 'Green',
children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
},
{
name: 'Orange',
children: [{ name: 'Pumpkins' }, { name: 'Carrots' }],
},
],
},
];
// Call the function to add the new object
addObjectBefore(data, 'Pumpkins', { name: 'new name' });
// Output the modified data
console.log(data);
答:
2赞
Srushti Shah
7/14/2023
#1
function addObjectBefore(arr, targetName, newObj) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].children) {
const targetIndex = arr[i].children.findIndex(obj => obj.name === targetName);
if (targetIndex !== -1) {
arr[i].children.splice(targetIndex, 0, newObj); // Insert new object before the target
return; // Stop the iteration once the target is found and object is inserted
} else {
addObjectBefore(arr[i].children, targetName, newObj); // Recursive call for nested children array
}
}
}
}
const data = [
{
name: 'Fruit',
children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
},
{
name: 'Vegetables',
children: [
{
name: 'Green',
children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
},
{
name: 'Orange',
children: [{ name: 'Pumpkins' }, { name: 'Carrots' }],
},
],
},
];
addObjectBefore(data, 'Pumpkins', { name: 'new name' });
// Output the modified data
console.log(data);
评论
0赞
panshul yogesh
7/14/2023
感谢 Shah @Srushti但是如果我想在 name:'Fruit' 之前添加新对象,不仅在嵌套的地方,而且在基于名称匹配的数组中的任何地方,您能否为此提供更新的代码。
0赞
panshul yogesh
7/14/2023
此外,名称始终是唯一的。忘了提了。
0赞
Mahadev Mirasdar
7/15/2023
好答案 加了 赞成 @Srushti Shah
0赞
Alexander Nenashev
7/14/2023
#2
调用后,您应该实际检查是否找到了索引,它应该是:Array::findIndex
>= 0
const data = [
{
name: 'Fruit',
children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
},
{
name: 'Vegetables',
children: [
{
name: 'Green',
children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
},
{
name: 'Orange',
children: [{ name: 'Pumpkins' }, { name: 'Carrots' }],
},
],
},
];
insert(data, 'Pumpkins', {name: 'newname'});
console.log(data);
function insert(data, name, what){
for(const child of data){
if(child.children){
const foundIdx = child.children.findIndex(item => item.name === name);
if(foundIdx >= 0){
child.children.splice(foundIdx + 1, 0, what);
return true;
}
if(insert(child.children, name, what)){
return true;
}
}
}
}
评论
0赞
panshul yogesh
7/14/2023
谢谢@Alexander但是如果我想在 name:'Fruit' 或 name:'Vegetables' 之前添加新对象,但基于名称匹配的数组中的任何位置,您能否为此提供更新的代码。
0赞
panshul yogesh
7/14/2023
此外,名称始终是唯一的。忘了提
评论