Get array's depth in JavaScript

前端 未结 6 2054
慢半拍i
慢半拍i 2020-12-11 08:25

In order to get the array\'s depth I thought I can use the flat() method like so:

相关标签:
6条回答
  • 2020-12-11 08:37

    I think a recursive approach is simpler. If your current item is an Array determine the max depth of its children and add 1.

    function getArrayDepth(value) {
      return Array.isArray(value) ? 
        1 + Math.max(...value.map(getArrayDepth)) :
        0;
    }
    
    
    
    let testRy = [1,2,[3,4,[5,6],7,[8,[9,91]],10],11,12]
    
    console.log(testRy);
    
    console.log(getArrayDepth(testRy))
    
    console.log(testRy);

    0 讨论(0)
  • 2020-12-11 08:37

    You can use a recursive function:

    function getArrayDepth(obj) {
        if (Array.isArray(obj)) return 1 + Math.max(...obj.map(t => getArrayDepth(t)))
        else return 0
    }
    
    
    console.log(getArrayDepth([1,2,[3,4,[5,6],7,[8,[9,91]],10],11,12]))
    console.log(getArrayDepth([1,[1]]))

    0 讨论(0)
  • 2020-12-11 08:40

    This one is a bit easier to understand, if you'd like.

    var array = [
    [0, 1],
    [1, 2, 3, [1, 0]],
    [2, 3, [1, 2, [5]]],
    [1, [6, 3, [1, 2, [1, 0]]]],
    [2]
    ]
    
    function depth(array, rec) {
    if (!Array.isArray(array)) throw new Exception('not an array');
    
    var res = rec;
    for(var i = 0; i < array.length; ++i) {
        if (Array.isArray(array[i])) {
        var subDepth = depth(array[i], rec + 1);
        if (subDepth > res) {
            res = subDepth;
        }
      }
    }
    return res;
    }
    
    0 讨论(0)
  • 2020-12-11 08:42

    I think you could use Infinity to flatten your array. MDN gives an example. Not sure how efficient this is though.

    const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
    
    arr4.flat(Infinity);
    
    // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    0 讨论(0)
  • 2020-12-11 08:47

    function test(arr) {
      return 1 + (arr instanceof Array ? arr.reduce(function(max, item) {
        return Math.max(max, test(item));
      }, 0) : -1);
    }
    
    
    
    let testRy = [1,2,[3,4,[5,6],7,[8,[9,91]],10],11,12];
    
    console.log(test(testRy));
    console.log(test([]));

    0 讨论(0)
  • 2020-12-11 08:47

    Convert the array to string lets assume We are given a string having parenthesis like below “( ((X)) (((Y))) )” We need to find the maximum depth of string, like 4 in above example. Since ‘Y’ is surrounded by 4 balanced parenthesis.

    Take two variables max and current_max, initialize both of them as 0. Traverse the string, do following for every character a) If current character is ‘(’, increment current_max and update max value if required. b) If character is ‘)’ means we previously had a ‘(’ character so decrement current_max without worry but dont reduce max value .

    If current_max is greater than max then update max value to current_max at that instance. after traverse is completed the max is is the depth of the array.

    0 讨论(0)
提交回复
热议问题