Array.prototype.flatMap()
Description
Is the same as calling [JavaScript Maps](JavaScript Maps) and then flattening the result. Ie:
arr.map(func).flat(1)
Type Signature
.flatMap(
callback: (value: T, index: number, array: T[]) => U|Array,
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’ ]