skip to Main Content

So, I have an array like this
const array=[1,2,3,4,[5,6],[[[[7,8]]]]]. I know that I can make it flat by flat function like this.
array.flat(4) // [1,2,3,4,5,6,7,8]

But as you know we can to this when we know array external count of this [] symbols. However we can fetch
any data that we don’t know how many [] this symbols are there in array. My question is that how to check
how many [] – this symbols are there within array.

2

Answers


  1. You can flatten an array of unknown depth with

    array.flat(Infinity).length
    

    You can count elements yourself though with a loop and a stack without recursion (no maximum call stack size error), that would be more efficient since you don’t create a new array:

    console.log(countNestedArrayItems([1,2,3,4,[5,6],[[[[7,8]]]]]));
    
    function countNestedArrayItems(arr) {
    
        let count = 0;
        const path = [];
    
        for (let i = 0; i < arr.length; i++) {
    
            const item = arr[i];
    
            if (Array.isArray(item)) {
                path.push(arr, i);
                i = -1;
                arr = item;
                continue;
            }
    
            count++;
            while(i === arr.length - 1 && path.length)  i = path.pop(), arr = path.pop();
    
        }
    
        return count;
    
    }

    And a benchmark:

    ` Chrome/124
    -----------------------------------------------------------------------------------
    >              n=6       |       n=60        |       n=600       |      n=6000     
    count    ■ 1.00x x1m 116 | ■ 1.00x x100k  94 | ■ 1.00x x100k 927 | ■ 1.00x x10k 860
    flat()     1.75x x1m 203 |   2.17x x100k 204 |   2.29x  x10k 212 |   2.60x  x1k 224
    -----------------------------------------------------------------------------------
    https://github.com/silentmantra/benchmark `
    
    const $chunk = [1,2,3,4,[5,6],[[[[7,8,[1,2,3,4,[5,6],[[[[7,8]]]]]]]]]];
    const $input = [];
    
    function countNestedArrayItems(arr) {
    
        let count = 0;
        const path = [];
    
        for (let i = 0; i < arr.length; i++) {
    
            const item = arr[i];
    
            if (Array.isArray(item)) {
                path.push(arr, i);
                i = -1;
                arr = item;
                continue;
            }
    
            count++;
            while(i === arr.length - 1 && path.length)  i = path.pop(), arr = path.pop();
    
        }
    
        return count;
    
    }
    // @benchmark count
    countNestedArrayItems($input);
    
    // @benchmark flat()
    $input.flat(Infinity).length;
    
    /*@skip*/ fetch('https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js').then(r => r.text().then(eval));
    Login or Signup to reply.
  2. Your question is not very clear, does this help?

    const array = [1, 2, 3, 4, [5, 6], [[[[7, 8]]]]];
    
    function countNestedArrays(arr) {
      let count = 0;
      for (let i = 0; i < arr.length; i++) {
        if (Array.isArray(arr[i])) {
          count++;
          count += countNestedArrays(arr[i]);
        }
      }
      return count;
    }
    
    console.log(countNestedArrays(array));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search