Skip to content

Instantly share code, notes, and snippets.

View MatthewTrout's full-sized avatar

Matthew MatthewTrout

View GitHub Profile
@MatthewTrout
MatthewTrout / flatten.test.js
Created October 24, 2019 11:48
es6 flatten test
import { flatten } from "./flatten";
test('flatten works', () => {
expect(
flatten(
[1, 2, [3, 4, 5], 6, [7], 8, 9, 10]
)
).toEqual(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
@MatthewTrout
MatthewTrout / flatten.js
Created October 24, 2019 11:47
es6 array flatten
export const flatten = (arr) => {
return arr.reduce((acc, curr) => {
const val = Array.isArray(curr) ? flatten(curr) : [curr];
return [...acc, ...val];
}, []);
};