Skip to content

Instantly share code, notes, and snippets.

@walterspieler
Created March 21, 2019 12:32
Show Gist options
  • Save walterspieler/72ce1638aff7f1a3a10d266d26bb7574 to your computer and use it in GitHub Desktop.
Save walterspieler/72ce1638aff7f1a3a10d266d26bb7574 to your computer and use it in GitHub Desktop.
Flatten an array with ES6
const multiDimensionalArray = [ [1, 2], [3, 4], [5, 6] ];
const flattenedArray =multiDimensionalArray.flat();
console.log(flattenedArray) // [1, 2, 3, 4, 5, 6]
// This methods accepts one argument to choose the depth of the flattening.
const multiDimensionalArray = [[ [1, 2]], [3, 4], [5, 6] ];
multiDimensionalArray.flat(2);
console.log(flattenedArray) // [1, 2, 3, 4, 5, 6]
const multiDimensionalArray = [ [1, 2], [3, 4], [5, 6] ]; 
const flattenedArray = [].concat(…multiDimensionalArray); 
console.log(flattenedArray) // [1, 2, 3, 4, 5, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment