There is one issue with the 2nd example used in the flatmap. The .flat() call at the end isn't needed, if you run the following code you will see that it evaluates to true
JSON.stringify(myAwesomeArray.map(arr => arr * 10)) === JSON.stringify( myAwesomeArray.map(arr => arr * 10).flat())
The issue is that if you execute [1] * 10 it will return 10 and not [10] as you would expect
[1] * 10
10
[10]
There is no issue with that code. It's just two examples. The first is used with flatMap()
flatMap()
const myAwesomeArray = [[1], [2], [3], [4], [5]] myAwesomeArray.flatMap(arr => arr * 10) //-------> Output : [10, 20, 30, 40, 50]
And the second uses flat() and map().
flat()
map()
const myAwesomeArray = [[1], [2], [3], [4], [5]] // With .map() and .flat() myAwesomeArray.map(arr => arr * 10).flat() //-------> Output : [10, 20, 30, 40, 50]
As a side note, with flatMap(), the map() function is applied first and flat() comes after.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
There is one issue with the 2nd example used in the flatmap. The .flat() call at the end isn't needed, if you run the following code you will see that it evaluates to true
The issue is that if you execute
[1] * 10
it will return10
and not[10]
as you would expectThere is no issue with that code. It's just two examples.
The first is used with
flatMap()
And the second uses
flat()
andmap()
.As a side note, with
flatMap()
, themap()
function is applied first andflat()
comes after.