Created
March 21, 2019 12:32
-
-
Save walterspieler/72ce1638aff7f1a3a10d266d26bb7574 to your computer and use it in GitHub Desktop.
Flatten an array with ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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