const example1 = [[1, 1, 1, [1.4, 1.6]], 666, [2, 3, [[4], [5, 6]]]]; function flat(arr) { const stack = arr; const result = []; while (stack.length) { const current = stack.shift(); if (Array.isArray(current)) { stack.unshift(...current); } else { result.push(current); } } return result; } console.assert(`${flat(example1)}`, '1,1,1,1.4,1.6,666,2,3,4,5,6');