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 Web Docs
However, its elements must be arrays if they are to be flattened. ... const arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4] const arr2 = [1, 2, [3, 4, [5, 6]]]; arr2.flat(); // [1, 2, 3, 4, [5, 6]] const arr3 = [1, 2, [3, 4, [5, 6]]]; arr3.flat(2); // [1, 2, 3, 4, 5, 6] const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]; arr4.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ... const arr5 = [1, 2, , 4, 5]; console.log(arr5.flat()); // [1, 2, 4, 5] const array = [1, , 3, ["a", , "c"]]; console.log(array.flat()); // [ 1, 3, "a", "c" ] const array2 = [1, , 3, undefined, ["a", , ["d", , "e"]], null]; console.log(array2.flat()); // [ 1, 3, undefined, "a", ["d", empty, "e"], null ] console.log(array2.flat(2)); // [ 1, 3, undefined, "a", "d", "e", null ]
🌐
SamanthaMing
samanthaming.com › tidbits › 71-how-to-flatten-array-using-array-flat
Flatten Array using Array.flat() in JavaScript | SamanthaMing.com
Here is a ES6 solution. This only work for one level nested array. const oneLevelDeep = [[1, 2], [3]]; const flattened = [].concat(...oneLevelDeep); // [1, 2, 3,]
🌐
Medium
medium.com › swlh › how-to-flatten-an-array-in-javascript-6b3fef075655
How to Flatten an Array in JavaScript | by Devin Michael Gray | The Startup | Medium
December 16, 2020 - // non recursive flatten deep using ... // reverse to restore input order return res.reverse(); }const arr = [1, 2, [3, 4, [5, 6]]]; flatten(arr); // [1, 2, 3, 4, 5, 6]...
🌐
4Geeks
4geeks.com › how-to › javascript-array-flatten
How to flatten an array in JavaScript?
July 16, 2025 - If you pass a number of layers to flatten for example 2, this method will flatten the first and the second layers of the array, in our example the first layer of numbers is [4, 5, [6, 7, [8]], 9] and the second layer is [6, 7, [8]]. Our example ...
🌐
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
🌐
Programiz
programiz.com › javascript › library › array › flat
JavaScript Array flat() (with Examples)
// reducing nesting by flattening the array to depth 2 let flattenArray = numbers.flat(2); // new flatten array console.log(flattenArray); // Output: // [ 1, 2, 3, 4, 5, 6, [ 7, 8 ] ] ... Here, arr is an array. ... Returns a flatted array with the sub-array elements concatenated into it.
🌐
freeCodeCamp
freecodecamp.org › news › flat-and-flatmap-javascript-array-methods
How to Use the flat() and flatMap() Methods to Flatten Arrays in JavaScript
July 26, 2022 - In this article I'm going to explain how to use the new array methods introduced in ES2019 (EcmaScript 2019) – flat() and flatMap(). You use these methods to flatten arrays. The methods are very useful and easy to use. It will really be cool to use these methods in your next project. Grab a coffee and let's dive into it. You should be familiar with the concept of arrays in JavaScript to follow along with this tutorial.
Find elsewhere
🌐
Flexiple
flexiple.com › javascript › flatten-array-javascript
JavaScript: Flatten an array using different methods - Flexiple
Here when we use the spread operator on the array arr we declared above, the Spread syntax (...) allows the concatenation operation to be performed on all the elements of the array and stores the result in an empty array thus giving us a flattened array.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › flat
JavaScript Array flat() - Flatten Nested Arrays | Vultr Docs
November 28, 2024 - This code takes a nested array and flattens it one level. The output will be [1, 2, [3, 4]]. Determine the depth of nested arrays you want to flatten.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.flat()
JavaScript Array flat()
November 7, 2024 - [ 'javascript', 'es6', 'html', 'css', 'react', 'redux', 'hooks' ]Code language: JavaScript (javascript) In this example, we use the map() method to return arrays of tags from the data array and call the flat() method to flatten the nested arrays.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › flatMap
Array.prototype.flatMap() - JavaScript - MDN Web Docs
... const arr = [1, 2, 3, 4]; arr.map((x) => [x * 2]); // [[2], [4], [6], [8]] arr.flatMap((x) => [x * 2]); // [2, 4, 6, 8] // only one level is flattened arr.flatMap((x) => [[x * 2]]); // [[2], [4], [6], [8]] While the above could have been achieved by using map itself, here is an example ...
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Array › flat
Array.prototype.flat() - JavaScript
const arr = [1, 2, [3, 4]]; // To flat single level array arr.flat(); // is equivalent to arr.reduce((acc, val) => acc.concat(val), []); // [1, 2, 3, 4] // or with decomposition syntax const flattened = arr => [].concat(...arr); const arr = [1, 2, [3, 4, [5, 6]]]; // to enable deep level flatten ...
🌐
Codedamn
codedamn.com › news › javascript
Flattening an Array: Best Practices and Examples
July 4, 2023 - Yes, you can flatten multi-level nested arrays in JavaScript. The flat() method can take a depth argument to handle this, or you can use a recursive function with reduce() and concat(). Flattening arrays is often necessary when handling real-world data structures. It simplifies the data and makes it easier to iterate over and manipulate. For more in-depth explanations and examples, you can visit the official MDN JavaScript Documentation.
🌐
Medium
medium.com › dailyjs › flatten-array-using-array-flat-in-javascript-ee4d0b2423e5
Flatten Array using Array.flat() in JavaScript | by Samantha Ming | DailyJS | Medium
September 1, 2019 - You simply have to set the appropriate depth parameter to flatten deeper nested arrays. const twoLevelsDeep = [[1, [2, 2], 1]];// depth = 1 twoLevelsDeep.flat() // [1, [2, 2], 1]// depth = 2 twoLevelsDeep.flat(2) // [1, 2, 2, 1] ... JavaScript ...
🌐
CodeBurst
codeburst.io › how-to-flatten-a-nested-javascript-array-628e01b85512
How to Flatten a Nested Javascript Array | by Cynthia Okoliezeh | codeburst
August 12, 2020 - The flat()method has an optional argument known as depth (the number of levels to flatten the array) and returns a new array - flattened version of the original array, removing any array hole (empty slot(s) in the array).
🌐
Dustin John Pfister
dustinpfister.github.io › 2021 › 07 › 15 › js-array-flat
Flatten an array with the flat method or alternatives | Dustin John Pfister at github pages
July 19, 2021 - When it comes to working with arrays in javaScript, and I want to flatten an array of arrays into a single array of values, and I am working in a modern javaScript environment, then I can use the flat Array prototype method to do so.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.flat()
Array.prototype.flat() - JavaScript Tutorial
November 7, 2024 - [ 'javascript', 'es6', 'html', 'css', 'react', 'redux', 'hooks' ]Code language: JavaScript (javascript) In this example, we use the map() method to return arrays of tags from the data array and call the flat() method to flatten the nested arrays.
🌐
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 - In this example, we used flatMap() to process each search term from the combined array and generate an array of suggestions. The final searchSuggestions array is flattened, making it suitable for displaying autocomplete suggestions in the search bar.