Skip to content

Instantly share code, notes, and snippets.

@nachogarcia
nachogarcia / Flatten in ES6
Created April 5, 2019 23:31
Flatten in ES6
const unflattened = [[1,2,[3]],4]
const flattened = [1, 2, 3, 4]
// It would be much easier with the already provided in the language: const result = unflattened.flat(Infinity)
const flatten = arr => arr.reduce((flat, next) => flat.concat(Array.isArray(next) ? flatten(next) : next), [])
const result = flatten(unflattened)
expect(result).toEqual(flattened)