Categories
JavaScript Answers

How to merge/flatten an array of arrays with JavaScript?

Spread the love

To merge/flatten an array of arrays with JavaScript, we call the flat method.

For instance, we write

const arrays = [["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]];
const merge3 = arrays.flat(1);
console.log(merge3);

to call arrays.flat with 1 to flatten the arrays array 1 level deep.

The returned flattened array is assigned to merge3.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.