loading...

re: 15 must-know JavaScript array methods in 2020 VIEW POST

FULL DISCUSSION
 

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

 

There is no issue with that code. It's just two examples.
The first is used with 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().

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.

code of conduct - report abuse