I can’t understand one thing in this tutorial, please see code below:
function steamrollArray(arr) {
const flattenedArray = [];
for (let i = 0; i < arr.length; i++) {
if(Array.isArray(arr[i])) {
flattenedArray.push(steamrollArray(arr[i]));
}
else {
flattenedArray.push(arr[i]);
}
}
return flattenedArray
}
steamrollArray([1, [2], [3, [[4]]]])
My question is: why code is not working without dots at the begining of recursive function call?
flattenedArray.push(steamrollArray(arr[i]))
If i wrote like this:
flattenedArray.push(...steamrollArray(arr[i]))
then everything works fine. As I understand, recursive function call should return same result and flattenedArray.push should simply push that result in array?