How to flatten a multi-dimensional array in JavaScript

Feb 19, 2019 Β· by Tim Kamanin

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