function steamrollArray(arr) {
const flattenedArray = [];
// Loop over array contents
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
// Recursively flatten entries that are arrays
// and push into the flattenedArray
flattenedArray.push(...steamrollArray(arr[i]));
} else {
// Copy contents that are not arrays
flattenedArray.push(arr[i]);
}
}
return flattenedArray;
};
// test here
steamrollArray([1, [2], [3, [[4]]]]);
Hello guys, I would like to know why in this code is used the spread operator and what it actually does, I can’t actually get it. Thanks!
Woah, you got recursion going on already… I still have to go back to that topic Nth times to completely understand it. Good job Mike, hope you’ll get this working.