You got me at
How is this useful ? It's not.
Here is my proposal for Array.prototype.flat.
Array.prototype.flat
const flatten = (target, level = 1) => { if (!Array.isArray(target)) { return target; } if (target.length === 0) { return []; } const [item, ...remainingItems] = target; if (!Array.isArray(item) || level === 0) { return [item, ...flatten(remainingItems, level)]; } return [...flatten(item, level - 1), ...flatten(remainingItems, level)]; }; console.log(flatten([1, 2, 3, [4, 5, [6, 7]]])); // [1, 2, 3, 4, 5, [6, 7]] console.log(flatten([1, 2, 3, [4, 5, [6, 7]]], Infinity)); // [1, 2, 3, 4, 5, 6, 7]
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You got me at
Here is my proposal for
Array.prototype.flat
.