Knowledgize

.flatMap(fn)

Maps each element using a mapping function and then flattens the result into a new array. It’s essentially the same as mapping followed by calling .flat() with depth 1.

Examples:

let nestedArrays = [[1, 2], [3, 4], [5, 6]]; let flatMapped = nestedArrays.flatMap(pair => pair.map(item => item * 2)); console.log(flatMapped); // [2, 4, 6, 8, 10, 12]
let phrases = ["it's Sunny in", "", "California"]; let words = phrases.flatMap(phrase => phrase.split(" ")); console.log(words); // ["it's", "Sunny", "in", "", "California"]