Created
October 18, 2017 17:54
-
-
Save dstreet/cb1915a67c44f4e23a2292a82882842a to your computer and use it in GitHub Desktop.
Flatten a JS array
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 assert = require('assert') | |
const flatten = arr => arr.reduce((acc, el) => { | |
return acc.concat(Array.isArray(el) ? flatten(el) : el) | |
}, []) | |
assert.deepEqual(flatten([1, 2, 3, 4]), [1, 2, 3, 4]) | |
assert.deepEqual(flatten([1, [2, 3], 4]), [1, 2, 3, 4]) | |
assert.deepEqual(flatten([1, [2, [3]], 4]), [1, 2, 3, 4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment