JavaScriptArrayflatMap
flat

flatMapArray

forEach

Maps each element using a function, then flattens the result by one level.

Syntax

arr.flatMap(callbackFn)

Example

Enter values below to update the example in real time.

const
sentences
words
flatMap
split
console
const sentences = ["Hello World", "Foo Bar"];
const words = sentences.flatMap((s) => s.split(" "));
console.log(words);   // ["Hello", "World", "Foo", "Bar"]