Created
February 20, 2018 17:24
-
-
Save aaronbeall/d0513f78204cf75d707141d516c65eb2 to your computer and use it in GitHub Desktop.
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 flatten = array => ( | |
array.reduce((accumulated, current) => accumulated.concat( | |
Array.isArray(current) ? flatten(current) : current | |
), []) | |
); | |
// Test | |
const before = [[1,2,[3]],4]; | |
const after = flatten(before); | |
const expected = [1,2,3,4]; | |
console.assert(after.length == expected.length, `Expected length ${after.length} to be ${expected.length}`) | |
console.assert(after.every((item, i) => expected[i] == item), `Expected array [${after}] to equal [${expected}]`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment