如何计算嵌套对象的深度?

发布于 2025-01-13 06:04:48 字数 652 浏览 1 评论 0原文

我有一个嵌套对象的示例数组:

let arr = [{id: 0, children: []},
           {id: 1, children:[
             {id: 2, children: []},
             {id: 3, children: [
               {id: 4, children: []} 
             ]}
           ]}
         ];

我需要计算每个对象的深度级别。在所有对象中我也有一个parentId 属性。

结果应该是:

let arr = [{id: 0, depth: 0, children: []},
           {id: 1, depth: 0, children:[
             {id: 2, depth: 1, children: []},
             {id: 3, depth: 1, children: [
               {id: 4, depth: 2, children: []} 
             ]}
           ]}
         ];

我也有一个平面结构中所有对象的数组。

解决方案?

I have an expample array of nested objects:

let arr = [{id: 0, children: []},
           {id: 1, children:[
             {id: 2, children: []},
             {id: 3, children: [
               {id: 4, children: []} 
             ]}
           ]}
         ];

I need to count depth level for each object. In all objects I have a parentId property too.

Result should be:

let arr = [{id: 0, depth: 0, children: []},
           {id: 1, depth: 0, children:[
             {id: 2, depth: 1, children: []},
             {id: 3, depth: 1, children: [
               {id: 4, depth: 2, children: []} 
             ]}
           ]}
         ];

I have an array of all the objects in a flat structure too.

Solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2025-01-20 06:04:48

只需创建一个接受数组和深度参数的函数,该参数将该深度添加到数组中的所有对象。然后以递增的深度在 children 数组上调用它:

let arr = [{id: 0, children: []},{id: 1, children:[{id: 2, children: []},{id: 3, children: [{id: 4, children: []} ]}]}];

function addDepth(arr, depth = 0) {
  arr.forEach(obj => {
    obj.depth = depth
    addDepth(obj.children, depth + 1)
  })
}

addDepth(arr)
console.log(arr)

Just make a function that takes an array and a depth parameter that adds that depth to all objects in an array. Then call it on the children array with an incremented depth:

let arr = [{id: 0, children: []},{id: 1, children:[{id: 2, children: []},{id: 3, children: [{id: 4, children: []} ]}]}];

function addDepth(arr, depth = 0) {
  arr.forEach(obj => {
    obj.depth = depth
    addDepth(obj.children, depth + 1)
  })
}

addDepth(arr)
console.log(arr)

纵情客 2025-01-20 06:04:48

您可以迭代数组并添加深度。

const
    depth = d => o => {
        o.depth = d;
        (o.children || []).forEach(depth(d + 1));
    };

let tree = [{ id: 0, children: [] }, { id: 1, children: [{ id: 2, children: [] }, { id: 3, children: [{ id: 4, children: [] }] }] }];

tree.forEach(depth(0));

console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could iterate the arrays and add a depth.

const
    depth = d => o => {
        o.depth = d;
        (o.children || []).forEach(depth(d + 1));
    };

let tree = [{ id: 0, children: [] }, { id: 1, children: [{ id: 2, children: [] }, { id: 3, children: [{ id: 4, children: [] }] }] }];

tree.forEach(depth(0));

console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

拧巴小姐 2025-01-20 06:04:48
function addDepth(arr, depth) {
  arr.map((entry) => {
    entry.depth = depth;
    addDepth(entry.children, depth+1);
  })
}

addDepth(arr, 0);
function addDepth(arr, depth) {
  arr.map((entry) => {
    entry.depth = depth;
    addDepth(entry.children, depth+1);
  })
}

addDepth(arr, 0);
眼中杀气 2025-01-20 06:04:48

您需要编写一个对您有用的函数。
试试这个。

function depth(o){
  var values;
  if (Array.isArray(o)) values = o;
  else if (typeof o === "object") values = Object.keys(o).map(k=>o[k]);
  return values ? Math.max.apply(0, values.map(depth))+1 : 1;
}

You need to write a function that would be count for you.
try this one.

function depth(o){
  var values;
  if (Array.isArray(o)) values = o;
  else if (typeof o === "object") values = Object.keys(o).map(k=>o[k]);
  return values ? Math.max.apply(0, values.map(depth))+1 : 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文