Flatten arrays easily with a single command. Or, go further than that by using a map function while flattening arrays.
Consider below example -
|
|
You can choose to iterate through all elements if you want to iterate through or access all numbers, or you could just use flat
.
|
|
A real-world example - you receive a file that may have a one or more comma-separated ‘student ids’ in a single column. One of the quickest way to process and sort those ids is by having them in a single array - so flat
ten them up.
|
|
You can then process the elements to apply a prefix or ‘do some such thing’. Alternatively, combine map
and flat
to do two things in one go.
|
|
flatMap
applies map
first, and flattens array on the mapped array.