When/why to use map/reduce over for loops

后端 未结 6 1179
情深已故
情深已故 2020-12-09 17:03

So I am getting into a bit of object manipulation in JavaScript for the first time and I have a question I\'m wondering if anyone could answer.

When I have an object

6条回答
  •  悲哀的现实
    2020-12-09 17:28

    forEach(): Executes a provided function(callback) once for each array element. Doesn’t return anything (undefined) but this callback is allowed to mutate the calling array.

    map(): Executes a provided function(callback) once for each array element and creates a new array with the results of this executions. It cannot mutate the calling array content.

    Conclution Use map() when you need to return a new array, use forEach() or for when you want to change the original array.

提交回复
热议问题