The flatMap() method is a combination of flat() and map() methods. This method initially maps each array element through a mapping function, then flatten up the array with depth value as 1.
callback- It is a function which produces an element for the newly created array and carries the following three arguments:
It returns a new array where each element is the result of the callback function.
Let's see the below examples to understand better:
It is a simple example to know the use of flatMap() method.Test it Now
Using flatMap() method with two different array elements.Test it Now
This example shows the name of the fruit which each member likes.Test it Now
An example to split the sentences into individual words.Test it Now
It is clear in the output that each sentence is seperated from one another and forming individual word.