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
Written by Codi
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#