JavaScript: Array.prototype.flatMap
New in JS2019.
arrayX.flatMap(f)
-
Apply function to every element of the array then flatten 1 level. Return the new array. Original array is not changed.
The function f is passed 3 args:
- currentElement
- currentIndex
- arrayX.
arrayX.flatMap(f, thisArg)
-
Use thisArg for this Binding of f. If it is not given,
undefined
is used.
const a1 = [[1,2],[[3],4]]; const a2 = a1.flatMap( (x => x[0]) ); console.log(a2); // [ 1, 3 ]