gpt4 book ai didi

javascript - 以动态深度访问嵌套数组

转载 作者:行者123 更新时间:2023-12-03 03:50:17 26 4
gpt4 key购买 nike

我有以下一段 json,它遵循特定的模式,但每个对象可以有任意数量的 children 并且每个 children > 对象可以有任意数量的子对象 ..

[
{
"name": "1st level",
"parentId": "",
"id": "dummyvalue1",
"children": [
{
"name": "2nd level child",
"parentId": "dummyvalue1",
"id": "dummyvalue2"
},
{
"name": "another 2nd level child",
"id": "dummyvalue3",
"children": [
{
"name": "3rd level child",
"id": "dummyvalue4",
"children": [
{
"name": "4th level child",
"parentId": "dummyvalue4",
"id": "dummyvalue5"
}
],
"parentId": "dummyvalue3"
}
],
"parentId": "dummyvalue1"
}
]
}
]

问题是,我只需要访问此嵌套对象/数组结构中特定深度的对象。

并且该深度是动态的。

如果深度等于3,那么我想在该级别的children数组中添加一个新的虚拟对象。所以新的结构看起来像这样..

[
{
"name": "1st level",
"parentId": "",
"id": "dummyvalue1",
"children": [
{
"name": "2nd level child",
"parentId": "dummyvalue1",
"id": "dummyvalue2"
},
{
"name": "another 2nd level child",
"id": "dummyvalue3",
"children": [
{
"name": "3rd level child",
"id": "dummyvalue4",
"children": [
{
"name": "4th level child",
"parentId": "dummyvalue4",
"id": "dummyvalue5"
}
],
"parentId": "dummyvalue3"
},
{
"name": "NEW OBJECT",
"parentId": "dummyvalue3",
"id": "random"
}
],
"parentId": "dummyvalue1"
}
]
}
]

有没有一种干净的方法可以做到这一点?而不是试图迭代到正确的级别的巨大循环。另外,我也不认为我可以使用点表示法,因为级别数和 深度 变量是动态的

最佳答案

我创建了这个函数:

function addChild(currentChild, childToAdd, depth) {
if (depth === 1) {
currentChild.children.push(childToAdd);
return;
}

const nextChild = currentChild.children.filter(child => child.children && child.children.length)[0];

if (!nextChild) {
if (currentChild.children && currentChild.children.length) {
currentChild.children[0].children = [childToAdd];
} else {
currentChild.children = [childToAdd];
}
return;
}

addChild(nextChild, childToAdd, depth - 1);
}

它需要 3 个参数:

  1. 当前 child
  2. 要添加的子项
  3. 剩余深度

如果深度为1,它只是将添加到。如果没有,它会查找 children 内部,使用第一个 children 并递归调用同一函数。

const data = [
{
"name": "1st level",
"parentId": "",
"id": "dummyvalue1",
"children": [
{
"name": "2nd level child",
"parentId": "dummyvalue1",
"id": "dummyvalue2"
},
{
"name": "another 2nd level child",
"id": "dummyvalue3",
"children": [
{
"name": "3rd level child",
"id": "dummyvalue4",
"children": [
{
"name": "4th level child",
"parentId": "dummyvalue4",
"id": "dummyvalue5"
}
],
"parentId": "dummyvalue3"
}
],
"parentId": "dummyvalue1"
}
]
}
]

function addChild(currentChild, childToAdd, depth) {
if (depth === 1) {
currentChild.children.push(childToAdd);
return;
}

const nextChild = currentChild.children.filter(child => child.children && child.children.length)[0];

if (!nextChild) {
if (currentChild.children && currentChild.children.length) {
currentChild.children[0].children = [childToAdd];
} else {
currentChild.children = [childToAdd];
}
return;
}

addChild(nextChild, childToAdd, depth - 1);
}

addChild(data[0], {foo: 'bar'}, 3);
addChild(data[0], {bar: 'baz'}, 2);
addChild(data[0], {baz: 'foo'}, 1);
addChild(data[0], {extraChild: 'foo'}, 4);


console.log(data);

关于javascript - 以动态深度访问嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45200391/

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