How to flatten a multi-dimensional array in JavaScript

19 Feb ยท by Tim Kamanin ยท 1 min read

Coming from Python which is considered to be the data-science language I'm very pleased with JavaScript's data-crunching functions. They are just succinct and neat!

Take the one for example, here's how you flatten a two-dimensional array:

const nestedArray = [['๐Ÿ‘', '๐Ÿ'], ['๐Ÿ‘Ž', '๐Ÿ˜'], ['๐Ÿ‘', 'โžก']];
const flatArray = [].concat(...nestedArray); 
console.log(flatArray) // [ '๐Ÿ‘', '๐Ÿ', '๐Ÿ‘Ž', '๐Ÿ˜', '๐Ÿ‘', 'โžก' ]

Want to get more ๐Ÿ”ฅ tips like this one?

Subscribe to get notified about new dev tutorials