JavaScript: Array.prototype.flatMap

By Xah Lee. Date: . Last updated: .

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:

  1. currentElement
  2. currentIndex
  3. 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 ]
BUY
ΣJS
JavaScript in Depth