JavaScript Tutorial

flatMap() Method

Maps values then flattens the result by one level.

const nested = [[1, 2], [3, 4]];
nested.flatMap(arr => arr); // [1, 2, 3, 4]
const nums = [1, 2, 3];
nums.flatMap(n => [n, n * 2]); // [1, 2, 2, 4, 3, 6]