Following code works fine to flatten deeply nested array
const arr = [[2, [4, 9]], 6, [5,32]];;
const finalArr = [];
function flattenArr(currArr) {
for (var el of currArr) {
if (Array.isArray(el)) {
finalArr.concat(flattenArr(el));
} else {
finalArr.push(el);
}
}
return finalArr;
}
flattenArr(arr);
console.log(finalArr); // Output:[2, 4, 9, 6, 5, 32]
But if I replace concat with push, it breaks and gives the following output:
[2, 4, 9, [circular object Array], [circular object Array], 6, 5, 32, [circular object Array]]
Can anyone help me understand why? Also, what does [circular object Array] mean?