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
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.
🌐
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 - 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 OPs on the end tends to be faster function flatten(input) { const stack = [...input]; const res = []; while(stack.length) { // pop value from stack const next = stack.pop(); if(Array.isArray(next)) { // push back array items, won't modify the original input stack.push(...next); } else { res.push(next); } } // reverse to restore input order return res.reverse(); }const arr = [1, 2, [3, 4, [5, 6]]]; flatten(arr); // [1, 2, 3, 4, 5, 6]
Discussions

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
Flatten a nested array without using the inbuilt flat() method (interview learning)
const flatten = (arr) => { const items = []; for (const x of arr) { if (Array.isArray(x)) { items.push(...flatten(x)); } else { items.push(x); } } return items; }; console.log(flatten([0, [1, 2, [33, 44]], [4, 5, 6, 7], [7, 8], 90])); More on reddit.com
🌐 r/learnjavascript
23
26
January 8, 2026
How to flatten this nested array
const result = array.map((_, i) => ({ name: array[0][i].name, age: array[1][i].age, job: array[2][i].job })) More on reddit.com
🌐 r/learnjavascript
17
2
July 20, 2022
Day 46: Can You Flatten a Deeply Nested Array in JavaScript?
Angular is Google's open source framework for crafting high-quality front-end web applications. r/Angular2 exists to help spread news, discuss current developments and help solve problems. Welcome · Create your account and connect with a world of communities More on reddit.com
🌐 r/Angular2
5
0
June 14, 2025
🌐
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.
🌐
4Geeks
4geeks.com › how-to › javascript-array-flatten
How to flatten an array in JavaScript?
July 16, 2025 - Flattening an array in JavaScript ... can be done in many different ways but the easiest way to achieve this is by using the JavaScript flat() method, as shown in the following example....
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.flat()
JavaScript Array flat()
November 7, 2024 - In this tutorial, you'll learn how to use the JavaScript Array flat() method to flatten an array with nested array to a specified depth.
🌐
Flexiple
flexiple.com › javascript › flatten-array-javascript
JavaScript: Flatten an array using different methods - Flexiple
Easily flatten arrays in JavaScript with our straightforward guide. Learn how to streamline your code and efficiently manipulate nested arrays.
Find elsewhere
🌐
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
🌐
ZetCode
zetcode.com › js-array › flat
JavaScript flat - flattening arrays in JS
April 4, 2025 - JavaScript flat tutorial shows how to flatten arrays in JavaScript. The tutorial provides numerous examples to demonstrate array flattening in JS.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.flat()
Array.prototype.flat() - JavaScript Tutorial
November 7, 2024 - In this tutorial, you'll learn how to use the JavaScript Array flat() method to flatten an array with nested array to a specified depth.
🌐
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.
🌐
SamanthaMing
samanthaming.com › tidbits › 71-how-to-flatten-array-using-array-flat
Flatten Array using Array.flat() in JavaScript | SamanthaMing.com
It was always complicated to flatten an array in JS. Not anymore! ES2019 introduced a new method that flattens arrays with Array.flat()...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-flat-method
JavaScript Array flat() Method - GeeksforGeeks
July 15, 2025 - The Javascript arr.flat() method ... in JavaScript creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. If no depth is provided, it defaults to 1. ... depth: It specifies, how deep the nested ...
🌐
YouTube
youtube.com › watch
How to Flatten an Array in JavaScript |Use flat(), Recursion & Other Methods to Handle Nested Arrays - YouTube
Working with nested arrays in JavaScript? In this beginner-friendly tutorial, you'll learn how to flatten an array in JavaScript using multiple methods like ...
Published   July 8, 2025
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › flatMap
Array.prototype.flatMap() - JavaScript - MDN Web Docs
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]]
🌐
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 ...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › flat
JavaScript Array flat() - Flatten Nested Arrays | Vultr Docs
November 28, 2024 - The flat() method in JavaScript ... depth. Define an array that contains nested arrays. Use the flat() method without specifying a depth to flatten the array one level deep....
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › javascript flatten array
JavaScript Flatten Array | 11 Amazing Examples of JavaScript Flatten Array
April 1, 2023 - Guide to JavaScript Flatten Array. Here we discuss the Introduction to JavaScript Flatten Array and its different Examples along with Code.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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).
🌐
Reddit
reddit.com › r/learnjavascript › learn how to flatten an array using the reduce method
r/learnjavascript on Reddit: Learn how to flatten an array using the Reduce Method
August 9, 2022 -

Ever wondered how to fatten an array in JavaScript using the reduce method? Check out this tutorial:

https://www.youtube.com/watch?v=wqJchgHQTwo

If you want more practice, check out the full course here:

https://www.udemy.com/course/cracking-the-javascript-coding-interview/?couponCode=03AFBAFA3BF8FF858384

Top answer
1 of 2
8
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.
2 of 2
1
You could probably reduce this down a little more. let arr = [1,2,3,4,[5,6,7,[8,9]]]; function flatten(array){ return Array.isArray(array) ? array.reduce((a, v)=>{ return a.concat(flatten(v)) },[]) : [array] } console.log(flatten(arr)) // [1,2,3,4,5,6,7,8,9] When dealing with recursion you always want to have a base case that prevents your function from going into an endless loop. Another important tip is to always return the same Type. Recursion can be difficult, so if you can make sure you always use the same type it will make things a lot easier. Then all we really need to do is Combine the arrays. Base Case Our main goal is to loop through the array and we need to check if the item is an array or anything else. function flatten(array){ if(Array.isArray(array)){ }else{ } } Same Type Since the end result will be an array, let's always return an array to simplify this. In our current code, the else statement isn't an array, so lets turn it into out. function flatten(array){ if(Array.isArray(array)){ return array }else{ return [array] } } Combining the Array We know we will have to return an array, but we also need to send any non-array elements back into the flatten function, so this means we need to loop through our current array. function flatten(array){ if(Array.isArray(array)){ let newArray = []; array.forEach((v)=>{ }); return newArray }else{ return [array] } } You might be tempted to check if this value is an array or not... but we already use this comparison as our base case, so we can just send the value into flatten again which will 100% return an array. function flatten(array){ if(Array.isArray(array)){ let newArray = []; array.forEach((v)=>{ flatten(v) }); return newArray }else{ return [array] } } Since an array is coming back we can then concat that this array to newArray, combining the 2 arrays into one. function flatten(array){ if(Array.isArray(array)){ let newArray = []; array.forEach((v)=>{ newArray.concat(flatten(v)) }); return newArray }else{ return [array] } } Since we are creating a new array and then looking through another array we can actually reduce this down using reduce. function flatten(array){ if(Array.isArray(array)){ let newArray = array.reduce((a,v)=>{ return a.concat(flatten(v)) },[]); return newArray }else{ return [array] } } There isn't really a reason to create a new array if we are sending it back, so we can clean this up. function flatten(array){ if(Array.isArray(array)){ return array.reduce((a,v)=>{ return a.concat(flatten(v)) },[]); }else{ return [array] } } Lastly, we can use a ternary operator instead of an traditional if statement. function flatten(array){ return Array.isArray(array) ? array.reduce((a, v)=>{ return a.concat(flatten(v)) },[]) : [array] }