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

后端 未结 12 1892
余生分开走
余生分开走 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条回答
  •  北海茫月
    2020-11-22 15:49

    forEach() :

    return value : undefined

    originalArray : not modified after the method call

    newArray is not created after the end of method call.

    map() :

    return value : new Array populated with the results of calling a provided function on every element in the calling array

    originalArray : not modified after the method call

    newArray is created after the end of method call.

    Since map builds a new array, using it when you aren't using the
    returned array is an anti-pattern; use forEach or for-of instead.
    

提交回复
热议问题