Javascript-Common-Challenges-Problems
Javascript-Common-Challenges-Problems copied to clipboard
Flatten array using recursion.
We can flatten multi dimensional array using recursion in this way.
const flatten_array = arr => { // Concat given array with an empty array let flatten = [].concat(...arr); // Check if the flatten array contains any further array // If so do recursion of that array else return the array return flatten.some(Array.isArray) ? flatten_array(flatten) : flatten; }
that looks nice and compact. Thanks @Ashwin7mak