JavaScriptJavaScript

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) // [ '๐Ÿ‘', '๐Ÿ', '๐Ÿ‘Ž', '๐Ÿ˜', '๐Ÿ‘', 'โžก' ]

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email โ†“ โ†“ โ†“