Created
September 4, 2018 10:50
-
-
Save MAKIO135/1fba099467a5d0701a8597024e235d66 to your computer and use it in GitHub Desktop.
flatten Arrays
This file contains hidden or 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
// https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript/39000004#39000004 | |
const flatten = function(arr, result = []) { | |
for (let i = 0, length = arr.length; i < length; i++) { | |
const value = arr[i]; | |
if (Array.isArray(value)) { | |
flatten(value, result); | |
} else { | |
result.push(value); | |
} | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment