profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

Array.prototype.flatMap()

Description

Is the same as calling JavaScript Maps and then flattening the result. Ie:

arr.map(func).flat(1)

Type Signature

.flatMap<U>(
  callback: (value: T, index: number, array: T[]) => U|Array<U>,
  thisValue?: any
): U[]

Syntax

console.log(["a", "b", "c"].flatMap((x) => x)) // [ 'a', 'b', 'c' ]
console.log(["a", "b", "c"].flatMap((x) => [x])) // [ 'a', 'b', 'c' ]
console.log(["a", "b", "c"].flatMap((x) => [[x]])) // [ [ 'a' ], [ 'b' ], [ 'c' ] ]
console.log(["a", "b", "c"].flatMap((x, i) => new Array(i + 1).fill(x))) // [ 'a', 'b', 'b', 'c', 'c', 'c' ]

Related