Get array's depth in JavaScript

前端 未结 6 2057
慢半拍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: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]
    

提交回复
热议问题