Array.prototype.flatMap

Calls a function on each element of an array, then flattens the result into a new array. This is identical to a map followed by flat with depth 1.

Syntax

flatMap(callback, thisArg?)

Parameters

Return value

A new array.

Example

[2, 3, 4, 5, 6].flatMap(x => [x, 2**x])
[2, 4, 3, 8, 4, 16, 5, 32, 6, 64]
var prices = {
  "tea": 2,
  "coffee": 5
}
Object.entries(prices).flatMap(([name, price]) => [name, '$'+price]).join(' ')
"tea $2 coffee $5"