JS: Flatten Nested Array

By Xah Lee. Date: . Last updated: .

To flatten nested array just 1 level, use this:

var aa = [1,[9,[3,7]],4];

// flatten array 1 level
console.log(
    Array.prototype.concat.apply([],aa)
); // [ 1, 9, [ 3, 7 ], 4 ]

Flatten Nested Array

Here's a function that flatten n levels of nested array.

const xah_flatten_array = ((array1, n) =>
{

/* [
 flatten nested array n levels. n default to 1
 n can be large, such as thousands. because when array no longer has element that's array, the function return it immediately

 http://xahlee.info/js/js_array_flatten.html
 version 2017-09-23
 copyright: free use, must include link and credit

 ] */

    if ( n === undefined ) { n = 1; }
    if ( n > 0 &&
         Array.prototype.some.call(array1, Array.isArray )
       ) {
        return xah_flatten_array(Array.prototype.concat.apply([],array1), n-1); }
    else {
        return array1; }
});

// -----------------------------------
// test

const aa = [0,[1,[2,[3,[4,[5,[6]]]]]], "xyz"];

console.log( aa );

console.log( xah_flatten_array(aa) );

console.log( xah_flatten_array(aa,1) );

console.log( xah_flatten_array(aa,2) );

console.log( xah_flatten_array(aa,3) );

console.log( xah_flatten_array(aa,4) );

console.log( xah_flatten_array(aa,400) );

// prints

// [ 0, [ 1, [ 2, [Array] ] ], 'xyz' ]
// [ 0, 1, [ 2, [ 3, [Array] ] ], 'xyz' ]
// [ 0, 1, [ 2, [ 3, [Array] ] ], 'xyz' ]
// [ 0, 1, 2, [ 3, [ 4, [Array] ] ], 'xyz' ]
// [ 0, 1, 2, 3, [ 4, [ 5, [Array] ] ], 'xyz' ]
// [ 0, 1, 2, 3, 4, [ 5, [ 6 ] ], 'xyz' ]
// [ 0, 1, 2, 3, 4, 5, 6, 'xyz' ]

Pre ES2015 Version

function xah_flatten_array (array1, n) {
    // flatten nested array n levels. n default to 1
    // n can be large, such as thousands. because when array no longer has element that's array, the function return it immediately
    // http://xahlee.info/js/js_array_flatten.html
    // version 2017-09-23
    // copyright: free use, must include link and credit

    if ( n === undefined ) { n = 1; }
    if ( n > 0 &&
         Array.prototype.some.call(array1, Array.isArray)
       ) {
        return xah_flatten_array(Array.prototype.concat.apply([],array1), n-1); }
    else {
        return array1; }
}

// -----------------------------------
// test

var aa = [0,[1,[2,[3,[4,[5,[6]]]]]], "xyz"];

console.log( aa );

console.log( xah_flatten_array(aa) );

console.log( xah_flatten_array(aa,1) );

console.log( xah_flatten_array(aa,2) );

console.log( xah_flatten_array(aa,3) );

console.log( xah_flatten_array(aa,4) );

console.log( xah_flatten_array(aa,400) );

// prints

// [ 0, [ 1, [ 2, [Array] ] ], 'xyz' ]
// [ 0, 1, [ 2, [ 3, [Array] ] ], 'xyz' ]
// [ 0, 1, [ 2, [ 3, [Array] ] ], 'xyz' ]
// [ 0, 1, 2, [ 3, [ 4, [Array] ] ], 'xyz' ]
// [ 0, 1, 2, 3, [ 4, [ 5, [Array] ] ], 'xyz' ]
// [ 0, 1, 2, 3, 4, [ 5, [ 6 ] ], 'xyz' ]
// [ 0, 1, 2, 3, 4, 5, 6, 'xyz' ]

Array Topic

  1. JS: Array Basics
  2. JS: Understand JS Array
  3. JS: Create Array
  4. JS: Sparse Array
  5. JS: Array-Like Object
  6. JS: Array How-To

  1. JS: Array Object
  2. JS: Array.prototype
Liket it? Put $1 at patreon.

Or, Buy JavaScript in Depth