Last Updated: May 23, 2017
·
792
· 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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside