Last Updated: May 23, 2017
·
316
· Codi

Flatten an array in Javascript or merge and flatten 2 arrays with flatMap

You can use is a an open source array-flatMap reusable component to build a new collection by applying a function to all elements of this array and using the elements of the resulting collections.

Example:

flatMap([[1, 2, 3], [4, 5, 6]], val => val) // => [1, 2, 3, 4, 5, 6]

It also has 4 tests passed:

flatMap() should flatten the multi-dimensional array to a single-dimensional one

flatMap() should flatten to a single-dimensional array without odd numbers

flatMap() should throw a type error in case the first argument is not an array

flatMap() should throw a type error in case the second argument is not a function