array.map() & array.forEach()的差異?

is there a difference between foreach and map?

http://stackoverflow.com/questions/354909/is-there-a-difference-between-foreach-and-map

從這篇擷取答案如下:


Different.
foreach iterates over a list and applies some operation with side effects to each list member (such as saving each one to the database for example)   我這邊補充: forEach回傳值是undefined。

map iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (such as converting a list of strings to uppercase)



另外補充的答案也擷取如下:

Short answer: map and forEach are different. Also, informally speaking, map is a strict superset of forEach.


Long answer: First, let's come up with one line descriptions of forEach and map:

    --> forEach iterates over all elements, calling the supplied function on each.

    --> map iterates over all elements, calling the supplied function on each, and produces a transformed array by remembering the result of each function call.


In many languages, forEach is often called just each. The following discussion uses JavaScript only for reference. It could really be any other language.

Now, let's use each of these functions.

Using forEach:

Task 1: Write a function printSquares, which accepts an array of numbers arr, and prints the square of each element in it.

Solution 1:

var printSquares = function (arr) {

    arr.forEach(function (n) {
        console.log(n * n);

    });

};


Using map:

Task 2: Write a function selfDot, which accepts an array of numbers arr, and returns an array wherein each element is the square of the corresponding element in arr.

Aside: Here, in slang terms, we are trying to square the input array. Formally put, we are trying to compute it's dot product with itself.

Solution 2:

var selfDot = function (arr) {

    return arr.map(function (n) {

        return n * n;

    });

};


How is map a superset of forEach?

You can use map to solve both tasks, Task 1 and Task 2. However, you cannot use forEach to solve the Task 2.


In Solution 1, if you simply replace forEach by map, the solution will still be valid. In Solution 2however, replacing map by forEach will break your previously working solution.


Implementing forEach in terms of map:

Another way of realizing map's superiority is to implement forEach in terms of map. As we are good programmers, we'll won't indulge in namespace pollution. We'll call our forEach, just each.

Array.prototype.each = function (func) {

    this.map(func);

};


Now, if you don't like the prototype nonsense, here you go:

var each = function (arr, func) {

    arr.map(func); // Or map(arr, func);

};


So, umm.. Why's does forEach even exist?

The answer is efficiency. If you are not interested in transforming an array into another array, why should you compute the transformed array? Only to dump it? Of course not! If you don't want a transformation, you shouldn't do a transformation.

So while map can be used to solve Task 1, it probably shouldn't. For each is the right candidate for that.



留言

熱門文章