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
You can flatten an array of unknown depth with
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:
And a benchmark:
Your question is not very clear, does this help?