Javascript-Common-Challenges-Problems icon indicating copy to clipboard operation
Javascript-Common-Challenges-Problems copied to clipboard

Flatten array using recursion.

Open Ashwin7mak opened this issue 5 years ago • 1 comments

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; }

Ashwin7mak avatar Mar 01 '19 22:03 Ashwin7mak

that looks nice and compact. Thanks @Ashwin7mak

rohan-paul avatar Mar 02 '19 07:03 rohan-paul