const out = []; function steamrollArray(arr) { for (let item of arr) { if (Array.isArray(item) === false) { out.push(item); } else if (Array.isArray(item)) { steamrollArray(item); } } return out; } console.log(steamrollArray([1, [2], [3, [[4]]]]));