Skip to content

Instantly share code, notes, and snippets.

@aaronbeall
Created February 20, 2018 17:24
Show Gist options
  • Save aaronbeall/d0513f78204cf75d707141d516c65eb2 to your computer and use it in GitHub Desktop.
Save aaronbeall/d0513f78204cf75d707141d516c65eb2 to your computer and use it in GitHub Desktop.
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