[removed] Difference between .forEach() and .map()

后端 未结 12 2015
余生分开走
余生分开走 2020-11-22 15:19

I know that there were a lot of topics like this. And I know the basics: .forEach() operates on original array and .map() on the new one.

I

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 15:55

    They are not one and the same. Let me explain the difference.

    forEach: This iterates over a list and applies some operation with side effects to each list member (example: saving every list item to the database)

    map: This iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (example: transforming list of strings to uppercase). It does not mutate the array on which it is called (although if passed a callback function, it may do so).

    References

    Array.prototype.forEach() - JavaScript | MDN

    Array.prototype.map() - JavaScript | MDN

提交回复
热议问题