What do you think about my solution? Flatten an array in JavaScript
It is interesting! There are a few points about your solution:
Array.isArray()
reducer
If I were going to define flatten with reduce, I would have written something like the following:
flatten
function flatten(xs){ if(xs.length === 0) return []; return xs.reduce((acc, x) => [...acc, ...(Array.isArray(x)? flatten(x): [x])], []); }
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.
What do you think about my solution? Flatten an array in JavaScript
It is interesting! There are a few points about your solution:
Array.isArray()
(here)reducer
function.If I were going to define
flatten
with reduce, I would have written something like the following: