When i log this , the elements of array are in a loop and the loop checks if the element is Array or not.
First is 1, which is a number so it executes the else statement ehich is pushing the elem into the filteredArr.
But when we go to second it is an array with one element 2 . Here the same function is executed with the arguement 2.
What the function should do is check if it is an array. As 2 is not an array so it should be pushed to filteredArr.
Here im getting confused and i looked upon it on google. And it suggested this. filteredArr.push(...(steamrollArray(elem))) to place instead of this (steamrollArray(elem)) .
It worked but i want to know why the one before doesn’t work and how is this
one working. I am not able to understand this part.
Thank you.
I understand now when we the function with the arg elem which in this case is 2 pushes elem into its own filteredArr which it later returns.
We can say the function works in its own domain or block and pushes the value in the filteredArr which is in its own block . When the function returns its own filteredArr it needs to be pushed to the we can say the main filteredArr. (steamrollArray(elem)) returns an array so we use ... rest parameter to push only its elements not the array.
Thanks