When I was responding to a question on Stack Overflow about flattening nested arrays, I provided a solution using recursive flattening.
var exampleArray = [ [1,2,3,4], [1,2,[1,2,3]], [1,2,3,4,5,[1,2,3,4,[1,2,3,4]]] ];
function findArrayLengths(input) {
return input.reduce((op,cur)=>{
return Array.isArray(cur) ? op.concat(findArrayLengths(cur)) : op.concat(cur)
},[])
}
let op = exampleArray.map(e=>{
return findArrayLengths(e).length
})
console.log(op);
However, I noticed that another code snippet also works well for deep flattening (flat with infinite depth). I looked into Array.prototype.Flat
var arr = [ [1,2,3,4], [1,2,[1,2,3]], [1,2,3,4,5,[1,2,3,4,[1,2,3,4]]], [[1,2,3,4], [1,2,[1,2,3]], [1,2,3,4,5,[1,2,3,4,[1,2,3,4]]]] ];
let op = arr.map(e=> e.flat(Infinity).length);
console.log(op);
So the question is: Is it a proper way to do deep flattening with flat like this, or are there any consequences? ?
You can find more information and the original question here.