I hope this helps

var twoDimension = [[1], [2], 3, 4, [5]];

var plano = twoDimension.reduce((acc, el) => acc.concat(el), []);

console.log(plano);

Answer from sonEtLumiere on Stack Overflow
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Flattened an array without using array.flat - JavaScript - The freeCodeCamp Forum
September 29, 2020 - Tell us what’s happening: So I just getting frustrated. I use the flat works like charm. However the the test are to use an alternative. the code is suppose to check for an array and the flatten the array until there is just one level. Its not working. Ugh Your code so far function steamrollArray(arr) { var flattened = []; for (let i = 0; i
🌐
Medium
thesumitshrestha.medium.com › how-to-flatten-an-array-in-javascript-with-and-without-an-inbuilt-function-3169c64c6b19
How to Flatten an Array in JavaScript with and without an inbuilt function? | by Sumit Shrestha | Medium
June 1, 2024 - How to Flatten an Array in JavaScript with and without an inbuilt function? Today, we’re going to talk about something really fun and cool in JavaScript. Imagine you have a box full of toys, and …
Discussions

javascript - Flatten an array by only one level without using .flat() - Stack Overflow
To flatten by a single level only, Array#concat() can be leveraged. It accepts any amount of arguments, so an array can be spread into the function call: ... This avoids any explicit loops. JavaScript handles everything: More on stackoverflow.com
🌐 stackoverflow.com
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
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
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
🌐
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 is generic. It only expects the this value to have a length property and integer-keyed properties. However, its elements must be arrays if they are to be flattened.
🌐
Reddit
reddit.com › r/learnjavascript › flatten a nested array without using the inbuilt flat() method (interview learning)
r/learnjavascript on Reddit: Flatten a nested array without using the inbuilt flat() method (interview learning)
January 8, 2026 -

In an interview, I was asked to flatten a nested array in JavaScript without using `flat()`.

Under pressure, I got stuck. Later, I realized it wasn’t that hard — I was just overthinking.

Here’s the recursive solution I wrote:

var array = [0, [1, 2, [33, 44]], [4, 5, 6, 7], [7, 8], 90];

var newArr = [];

function getFlatArray(array) {

for (let index = 0; index < array.length; index++) {

if (typeof array[index] === "number") {

newArr.push(array[index]);

} else {

getFlatArray(array[index]);

}

}

}

getFlatArray(array);

console.log(newArr);

(12) [0, 1*,* 2*,* 33*,* 44*,* 4*,* 5*,* 6*,* 7*,* 7*,* 8*,* 90*]*

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-flatten-array-with-the-javascript-jquery
How to flatten array with the JavaScript/jQuery? - GeeksforGeeks
July 12, 2025 - Flattening an array in JavaScript or jQuery involves transforming a nested array into a single-level array.
🌐
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
🌐
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 - As I was interviewing for PayPal, my recruiters kept stressing that I needed to know how to “flatten” an array. I recall this advice and my first thought was… “what does that even mean”? As the recruiter then explained, flattening an array is the process of taking nested array elements and basically putting them all into one “flat” 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!

🌐
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 - This is the old school way of flattening an array of any depth which was the go-to before flat() was introduced. See my explanation of reduce above. This method uses the same logic as that explanation, but instead of concatenating the currentValue with the accumulator, you first check if the currentValue is an 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] }
🌐
4Geeks
4geeks.com › how-to › javascript-array-flatten
How to flatten an array in JavaScript?
July 16, 2025 - There are different ways to flatten ... the JavaScript flat() method. Undoubtedly, the easiest and most effective way to flatten an array is by using the flat method, as its name indicates this method provides a very simple way to flatten an array no matter how many layers deep it is. ... As shown in this example, the flat() method can be called with different arguments, if you call this method without an argument ...
🌐
Codedamn
codedamn.com › news › javascript
Flattening an Array: Best Practices and Examples
July 4, 2023 - There are several ways to flatten an array in JavaScript. Let's explore some of them. The flat() method is a built-in JavaScript method that flattens the input array into a new array.
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions
JavaScript Challenge - Flatten an Array - Page 2 - Frequently Asked Questions - Codecademy Forums
October 26, 2022 - My attempt: function flattenArray(array) { // Write your code here let newArray = ; for (var i = 0; i
🌐
W3Schools
w3schools.com › jsref › jsref_array_flat.asp
JavaScript Array flat() Method
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
🌐
DEV Community
dev.to › mukul_singhal › deep-flatten-an-array-5elf
Deep Flatten an Array - DEV Community
November 16, 2021 - const arr = [1,[2,3,[4,5]]]; // Setting the depth value to // Infinity to deep flatten the array const flattened = arr.flat(Infinity); console.log(flattened) // Output [1,2,3,4,5] ... Now we will see how do it without using any builtin function or basically writing the pollyfill for flat function
🌐
Scaler
scaler.com › topics › javascript-flatten-array
Flatten an Array in JavaScript
May 4, 2023 - Scaler Topics offers free certificate courses for all programming languages like Java, Python, C, C++, DSA etc. Learn from top MAANG experts and level up your skills. Explore now
🌐
Medium
medium.com › @ndc6968 › flattening-arrays-without-using-flat-in-javascript-0517970b4ae0
Flattening Arrays Without Using .flat() in JavaScript | by Nina D'Cruz | Medium
July 31, 2025 - Flattening an array means converting a nested array (an array of arrays) into a single-level array. ... While .flat() is a built-in ES2019 method that works well. You might need to avoid it due to: Browser compatibility (older browsers like IE11 don’t support it). Interview challenges (you’re expected to solve problems without using shortcuts).