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

You might want to filter an array in JavaScript by passing the filter criteria and return the filtered array. In...
Assume that you have an angle in degree and you wish to convert it to radians in JavaScript so that...
If you have a decimal number in JavaScript and want to convert it to its equivalent octal , hexadecimal and...