简体   繁体   English

查找深嵌套数组深度

[英]Find deep nested array depth

I have a nested array.我有一个嵌套数组。 Like below: I want to find the depth of this nested array, which means the child element has most deep nested children.如下所示:我想找到这个嵌套数组的深度,这意味着子元素具有最深的嵌套子元素。

let arr = [
   {
     name: 'tiger',
     children: [{
       name: 'sinba',
       children: [{
         name: 'cute',
         children: []
        }]
      }]
    },
    {
      name: 'lion',
      children: []
    }
]

In this case, the depth is 3 , the tiger has 3 level.在这种情况下,深度为3tiger有 3 级。 So the depth is 3所以深度是3

How could i achieve this?我怎么能做到这一点? I try to use recursive, but don't know how to find the element which has most nested children.我尝试使用递归,但不知道如何找到具有最多嵌套子元素的元素。

Thanks in advance.提前致谢。

Assuming that there are no circular references, you could try something like this假设没有循环引用,你可以尝试这样的事情

 let arr = [{ name: 'tiger', children: [{ name: 'sinba', children: [{ name: 'cute', children: [] }] }] }, { name: 'lion', children: [] } ] function count(children) { return children.reduce((depth, child) => { return Math.max(depth, 1 + count(child.children)); // increment depth of children by 1, and compare it with accumulated depth of other children within the same element }, 0); //default value 0 that's returned if there are no children } console.log(count(arr))

Our function would not work if there were some circular references, so there might be a need to adjust it accordingly.如果有一些循环引用,我们的 function 将不起作用,因此可能需要相应地调整它。 Detecting circular references is a whole ordeal.检测循环引用是一个完整的考验。 If nothing is done about it, the function will throw a Maximum call stack size exceeded error.如果不采取任何措施,function 将抛出超出最大调用堆栈大小的错误。

In order to handle it without any additional functionality implementation you could use already existing native JSON.stringify to do so.为了在没有任何附加功能实现的情况下处理它,您可以使用已经存在的本机JSON.stringify来执行此操作。 The stringify option will throw an exception only if you try to serialize BigInt values which we can handle ourselves or when objects are cyclic, which is excatly what we wanted.仅当您尝试序列化我们可以自己处理的BigInt值或当对象是循环的时,stringify 选项才会抛出异常,这正是我们想要的。

 let arr = [{ name: 'tiger', children: [] }] function testCircular(arr){ try { BigInt.prototype.toJSON = function() { return this.toString() } // Instead of throwing, JSON.stringify of BigInt now produces a string JSON.stringify(arr); return false; } catch (e) { // will only enter here in case of circular references return true; } } function count(children) { if (testCircular(children)) return Infinity; return children.reduce((depth, child) => { return Math.max(depth, 1 + count(child.children)); // increment depth of children by 1, and compare it with accumulated depth of other children within the same element }, 0); //default value 0 that's returned if there are no children } console.log(count(arr)) // normally counting arr[0].children = arr; // creates circular reference console.log(count(arr)) // counting for circular

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM