Skip to content

Flatten array using recursion. #1

@Ashwin7mak

Description

@Ashwin7mak

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions