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
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 ]
Discussions

Best Ways to Deep Flatten Arrays in JavaScript?
What's wrong with just arr.flat(Infinity)? More on reddit.com
🌐 r/learnjavascript
11
0
January 2, 2025
How to flatten deeply nested Array of Objects & Arrays [SOLVED]
Hi guys, I thought this would be a pretty simple problem to solve, but I’m running in to some trouble flattening out an array of nested arrays and objects. I basically just want to flatten it out to one single array of values. Basically, I want to check if any of the array properties are ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
1
January 30, 2018
Learn how to flatten an array using the Reduce Method
arr.flat(); // Or... arr.flatMap(); Done. No need for reduce() or anything needlessly complicated. Just use the incredibly simple method already available to you instead of reinventing the wheel. More on reddit.com
🌐 r/learnjavascript
48
2
August 9, 2022
How do you convert nested Array of objects into flattened Array of objects?
You will have to sort the types but you can flatten nested iterators with https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.flatten More on reddit.com
🌐 r/rust
12
0
July 31, 2022
🌐
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 › 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 - If one of those elements happens to have an array, the process will repeat itself. Because we use push and pop to add elements in our result array, the array will be backwards, so we use the reverse method to flip our array back in the correct order. This is slightly more efficient than using shift and unshift, as those methods would force JavaScript to re-index the array after each iteration. // non recursive flatten deep using a stack // note that depth control is hard/inefficient as we will need to tag EACH value with its own depth // also possible w/o reversing on shift/unshift, but array
🌐
Reddit
reddit.com › r/learnjavascript › best ways to deep flatten arrays in javascript?
r/learnjavascript on Reddit: Best Ways to Deep Flatten Arrays in JavaScript?
January 2, 2025 -

Flattening arrays with .flat() is simple, but handling deeply nested arrays can be tricky. What’s your go-to approach for deep flattening in JavaScript? I came across this interesting write-up with some techniques: Deep Flatten Arrays in JavaScript. https://www.interviewsvector.com/javascript/deep-flaten

Would love to hear your thoughts or see alternative methods!

🌐
Gitbooks
schneidenbach.gitbooks.io › typescript-cookbook › content › functional-programming › flattening-array-of-arrays.html
Flattening array of arrays · TypeScript Cookbook
You can combine the power of a new Array, the concat function, and the spread operator to create a new array with all of the objects contained within it: const flattenedArray = [].concat(...nestedArrays); ... const flattenedArray: Person[] = [ {firstName: "Andrew", lastName: "Smith"}, {firstName: ...
🌐
freeCodeCamp
forum.freecodecamp.org › t › how-to-flatten-deeply-nested-array-of-objects-arrays-solved › 171690
How to flatten deeply nested Array of Objects & Arrays [SOLVED] - The freeCodeCamp Forum
January 30, 2018 - Hi guys, I thought this would be a pretty simple problem to solve, but I’m running in to some trouble flattening out an array of nested arrays and objects. I basically just want to flatten it out to one single array of values. Basically, I want to check if any of the array properties are ...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › flatMap
Array.prototype.flatMap() - JavaScript | MDN
Note that in this particular case the flatMap approach is slower than the for-loop approach — due to the creation of temporary arrays that must be garbage collected, as well as the return array not needing to be frequently resized. However, flatMap may still be the correct solution in cases where its flexibility and readability are desired. ... 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]]
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-merge-flatten-an-array-of-arrays-in-javascript
How to Merge/Flatten an array of arrays in JavaScript ? - GeeksforGeeks
July 23, 2025 - ... function flattenArray(arr) { // Initialize an empty array to hold // the flattened result let result = []; // Helper function to recursively flatten elements function flatten(element) { if (Array.isArray(element)) { // If the element is ...
🌐
GreatFrontEnd
greatfrontend.com › questions
Front End Interview Practice Questions | GreatFrontEnd
JavaScript functions · User interface ... frameworks · Users completed · 43.1k done · FlattenImplement a function that recursively flattens an array into a single level deep ·...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › slice
Array.prototype.slice() - JavaScript | MDN
The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
🌐
Mtm
mtm.dev › flattening-multidimensional-javascript-arrays
How to flatten multidimensional JavaScript arrays
June 22, 2018 - const deepArray = [ [['Dolores', 'Wyatt'], 'Teddy'], [['William', 'Man In Black'], 'Emily'], [['Bernard', 'Arnold'], 'Charlie'], [['Ford', 'Fordnard'], 'Maeve'] ] // Flatten deep arrays function flatten(x) { return Array.isArray(x) ? [].concat(...x.map(flatten)) : x } console.log(flatten(deepArray)) // ['Dolores', 'Wyatt', 'Teddy', ...] concat is just a method for joining arrays together.
🌐
Medium
akhtarvahid.medium.com › flatten-nested-array-recursively-array-flat-in-javascript-5350f9d6f08d
Flatten nested array recursively | Array.flat() in JavaScript | by Vahid Akhtar | Medium
June 30, 2023 - The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. ... const hearts = ["💜",["💙"],["❤️‍🩹"]]; const flattened = heart.flat(); console.log(flattened); // ...
🌐
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.
🌐
DEV Community
dev.to › karthikraji2020 › flattening-arrays-in-javascript-41m5
Flattening Arrays In Javascript - DEV Community
July 6, 2020 - arrays = arrays.reduce(function(a, b){ return a.concat(b); }, []); ... // Flatten 2 levels deep const arr3 = [2, 2, 5, [5, [5, [6]], 7]]; arr3.flat(2); // [2, 2, 5, 5, 5, [6], 7]; // Flatten all levels const arr4 = [2, 2, 5, [5, [5, [6]], 7]]; ...
🌐
The Globe and Mail
theglobeandmail.com › culture › art & architecture › the cca unpacks chinese architecture’s secret history and toronto’s shift to digital technology
The CCA unpacks Chinese architecture’s secret history and Toronto’s shift to digital technology - The Globe and Mail
2 weeks ago - The array of LaserJet printouts and ideas for the early web, which he created with Toronto-based software studio Alias, carry a nerdy, cheerful optimism about the future: The new information superhighway was opening new territories of perception, and would remake both digital and physical space.
🌐
Jsontotable
jsontotable.org
JSON to Table Converter - Convert Complex Nested JSON to HTML Table Online
Transform complex nested JSON data into clean HTML tables instantly with unlimited nesting depth support. Excel export, tree structure visualization, real-time editing, and nested structure support.
🌐
Substack
datascience22.substack.com › p › top-10-numpy-interview-questions
Top 10 Numpy Interview Questions You Should Be Ready to Answer
July 30, 2024 - Question: How do you reshape and flatten a NumPy array? Answer: You can change the shape of an array using the reshape() method, and flatten it using the flatten() method or ravel() function.