How to Flatten a 2-D array in JavaScript ?

You can flatten a 2-D array in JavaScript using the concat and apply method as shown in the below code snippet.

The first step is to merge the 2-D array in to 1-D array using the concat method and then use the apply method to flatten it by passing the array of arguments.

How to Flatten a 2-D array in JavaScript ?

var programminglanguages = [];
programminglanguages[0] = ['C#', 'VB.NET'];
programminglanguages[1] = ['F#', 'Java', 'Oxygene.NET'];
var flattenedArray = programminglanguages.concat.apply([], programminglanguages);
console.log(flattenedArray);

image

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

JavaScript supports different types of operators that includes Assignment Operator JavaScript supports different types if Assignment Operators like = +=...
Assume that you have an angle in degree and you wish to convert it to radians in JavaScript so that...
The JavaScript elements and statements are placed within the script tag within the HTML page. You can specify the language...