title: Get array's depth in JavaScript
body: <p>In order to get the array's depth I thought I can use the <code>flat()</code> method like so:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function getArrayDepth(ry){
    // number of levels: how deep is the array
    let levels = 1;
    // previous length
    let prev_length = 1;
    // current length
    let curr_length = ry.length;
    //if the resulting array is longer than the previous one  add a new level
    while(curr_length &gt; prev_length){
    ry = ry.flat();
    prev_length = curr_length
    curr_length = ry.length;
    levels ++
    }
    return levels;
  }
  
  
  
  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);</code></pre>
  </div>
  </div>
  </p>
  
  <p>It seams it works BUT if one of the arrays inside has a length of 1 </p>
  
  <blockquote>
    <p>let testRy = [1, 2, [3, 4, [5, 6], 7, [8, <strong>[9]</strong> ], 10], 11, 12]</p>
  </blockquote>
  
  <p>the function fails since the flattened array is as long as the previous one.</p>
  
  <p>Is there a better way to get the depth of an array in javascript? </p>