DEV Community

[Comment from a deleted post]
Collapse
 
ashutoshbw profile image
Ashutosh Biswas

In the recursive version, JavaScript produces SyntaxError(It would be nice if it wouldn't) cause ternary operator does not allow spreading like this. We can solve it using spread operators if we really want to, but I've found concat requires a little less typing:

const flatten = (arr) => arr.reduce((prev, c) => prev.concat(Array.isArray(c) ? flatten(c) : c), [])
Enter fullscreen mode Exit fullscreen mode