ES2019

ES2019 introduced the Array.prototype.flat() method which you could use to flatten the arrays. It is compatible with most environments, although it is only available in Node.js starting with version 11, and not at all in Internet Explorer.

const arrays = [
      ["12"],
      ["25"],
      ["22"],
      ["$10"]
    ];
const merge3 = arrays.flat(1); //The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
console.log(merge3);
    


Older browsers

For older browsers, you can use Array.prototype.concat to merge arrays:

var arrays = [
  ["12"],
  ["25"],
  ["22"],
  ["$10"]
];
var merged = [].concat.apply([], arrays);

console.log(merged);

Using the apply method of concat will just take the second parameter as an array, so the last line is identical to this:

var merged = [].concat(["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]);
Answer from Gumbo on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › flat
Array.prototype.flat() - JavaScript | MDN
The flat() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length. If the element is not an array, it's directly appended to the result. If the element is an array, it's flattened according to the depth parameter.
🌐
W3Schools
w3schools.com › jsref › jsref_array_flat.asp
JavaScript Array flat() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
🌐
Medium
medium.com › nerd-for-tech › exploring-array-flattening-techniques-in-javascript-8167b51a69d4
Exploring Array Flattening Techniques in JavaScript | by Beck Moulton | Nerd For Tech | Medium
December 20, 2023 - Exploring Array Flattening Techniques in JavaScript Array flattening refers to the process of converting a multi-layer nested array into a one-dimensional array. In a multi-layer nested array …
🌐
Wisdom Geek
wisdomgeek.com › development › web-development › javascript › flatten-arrays-in-vanilla-javascript-with-flat-and-flatmap
Flatten Arrays in Vanilla JavaScript with flat() and flatMap() - Wisdom Geek
October 10, 2023 - As the name suggests, the flat() method returns a new array with elements of the subarray flattened into it, that is the sub-elements are concatenated into a single array. [[1, 2], [3, 4], [5, 6]].flat(); // [1, 2, 3, 4, 5, 6]JavaScript
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › flattening-multi-dimensional-arrays-in-javascript
Flattening multi-dimensional arrays in JavaScript
March 15, 2026 - Default is 1. Use Infinity to flatten all levels. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Array Flattening Demo</title> </head> <body> <h2>Flattening Multi-dimensional Arrays</h2> <div id="output"></div> <script> let output = document.getElementById('output'); // Original nested array let nestedArray = [1, 2, 3, [4, 5], [6, [7, 8]]]; output.innerHTML += '<p><strong>Original:</strong> ' + JSON.stringify(nestedArray) + '</p>'; // Flatten by depth 1 (default) let flat1 = nestedArray.flat(); output
🌐
CoreUI
coreui.io › answers › how-to-flatten-a-nested-array-in-javascript
How to flatten a nested array in JavaScript · CoreUI
September 20, 2025 - Use the flat() method to convert nested arrays into a single-level array with modern ES2019 syntax in JavaScript.
🌐
ZetCode
zetcode.com › js-array › flat
JavaScript flat - flattening arrays in JS
April 4, 2025 - The flat() method returns a new array with one level of nesting removed. ... The flat method can take a depth parameter to control flattening levels.
🌐
DEV Community
dev.to › lucasjstifano › flattening-arrays-with-flat-and-flatmap-in-javascript-omo
Flattening Arrays with flat() and flatMap() in JavaScript - DEV Community
October 25, 2024 - The flat() method is used to create a new array by recursively flattening the input array up to a specified depth.
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-use-the-javascript-flat-method-to-flatten-arrays-efficiently-426805bececf
How to Use the JavaScript flat() Method to Flatten Arrays Efficiently | by Codecupdev | JavaScript in Plain English
September 16, 2024 - JavaScript provides us with many methods for working with the arrays. One of these is the flat() method. The method was introduced in ES2019. It helps us to flatten arrays which are nested. Overall, this means the code can be more readable and extensible.
🌐
Medium
claire-deboer.medium.com › how-to-flatten-an-array-in-javascript-5650ad6a7b8f
How to Flatten An Array in JavaScript | by Claire DeBoer | Medium
June 9, 2021 - Introduced as of ES2019, flat() returns a new array which is the flattened version of the original, removing any empty slots in the array. It works on most modern browsers. There is an optional argument known as depth which is the number of ...
Top answer
1 of 16
76

Perfect use case for recursion, which could handle even deeper structure:

function flatten(ary) {
    var ret = [];
    for(var i = 0; i < ary.length; i++) {
        if(Array.isArray(ary[i])) {
            ret = ret.concat(flatten(ary[i]));
        } else {
            ret.push(ary[i]);
        }
    }
    return ret;
}

flatten([[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]]) // [0, 1, 2, 3, 4, 5]

Alternatively, as an Array method:

Array.prototype.flatten = function() {
    var ret = [];
    for(var i = 0; i < this.length; i++) {
        if(Array.isArray(this[i])) {
            ret = ret.concat(this[i].flatten());
        } else {
            ret.push(this[i]);
        }
    }
    return ret;
};

[[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]].flatten() // [0, 1, 2, 3, 4, 5]

EDIT #1: Well, think it a little bit functional way (except for the named recursion which should be using Y-combinator for pure functional :D).

function flatten(ary) {
  return ary.reduce(function(a, b) {
    if (Array.isArray(b)) {
      return a.concat(flatten(b))
    }
    return a.concat(b)
  }, [])
}

Let's adopt some ES6 syntax which makes it even shorter, in one line.

const flatten = (ary) => ary.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), [])

But remember, this one cannot be applied as an array method, because arrow functions don't have theirs own this.


EDIT #2: With the latest Array.prototype.flat proposal this is super easy. The array method accepts an optional parameter depth, which specifies how deep a nested array structure should be flattened (default to 1).

[[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]].flat()  // [[[[0]], [1]], [[[2], [3]]], [[4], [5]]]
[[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]].flat(2) // [[[0]], [1], [[2], [3]], [4], [5]]
[[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]].flat(3) // [[0], 1, [2], [3], 4, 5]
[[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]].flat(4) // [0, 1, 2, 3, 4, 5]

So to flatten an array of arbitrary depth, just call flat method with Infinity.

[[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]].flat(Infinity) // [0, 1, 2, 3, 4, 5]
2 of 16
53

ES6-style with recursion:

Redacted

June 2018 Update:

There is now an ES proposal for an Array.prototype.flat method. It is currently at stage 3, meaning it's likely to be implemented by browsers soon(ish) and make it into the spec in its current form. There are probably some polyfills floating around.

Example:

const nested = [[[0], [1]], [[2], [3]], [[4], [5]]];
const flattened = nested.flat(2);  // Need to specify depth if > 1

June 2019 Update:

Array.prototype.flat was officially added to the language in the ES2019 spec.

🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.flat()
JavaScript Array flat()
November 7, 2024 - ES2019 introduced the Array.prototype.flat() method that creates a new array with all the elements of the subarrays concatenated to it recursively up to a specified depth. ... depth : This optional parameter specifies how deep the method should ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-flat-method
JavaScript Array flat() Method - GeeksforGeeks
July 15, 2025 - The Javascript arr.flat() method was introduced in ES2019. The flat() method in JavaScript creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.