Search This Blog

2026/08/10

Javascript:Find all words in array of sentences



find all words in array of sentences.

Code:
const sentences = [
    "I love JavaScript",
    "flatMap is useful"
];

let flattened = sentences.flatMap(item=>item.split(" "))
console.log( flattened)

Output:
[ 'I', 'love', 'JavaScript', 'flatMap', 'is', 'useful' ]

without flatMap same ouput can be obtained by first map then flat

const sentences = [
    "I love JavaScript",
    "flatMap is useful"
];

let flattened = sentences.map(item=>item.split(" ")).flat()
console.log(flattened);

No comments:

Post a Comment