Home > Web Front-end > JS Tutorial > body text

Recommended forEach, $.each, and map methods in JS_javascript skills

WBOY
Release: 2016-05-16 15:06:41
Original
1451 people have browsed it

forEach is the most basic one of the new Array methods in ECMA5, which is traversal and looping. For example, the following example:

[1, 2,3, 4].forEach(alert);

Equivalent to the following for loop

var array = [1, 2, 3, 4];
for (var k = 0, length = array.length; k < length; k++) {
 alert(array[k]);
}
Copy after login

Array In the new method of ES5, the parameters are all function types, and parameters are passed by default. The function callback in the forEach method supports 3 parameters. The first one is the traversed array content; the second one is the corresponding array. Index, the 3rd one is the array itself.

Therefore, we have:

[].forEach(function(value, index, array) {
  // ...
});
Copy after login

Compare the $.each method in jQuery:

$.each([], function(index, value, array) {
  // ...
});
Copy after login

You will find that the first and second parameters are exactly opposite. Please pay attention and don’t remember them wrongly. The same is true for similar methods later, such as $.map.

var data=[1,3,4] ; 
var sum=0 ;
data.forEach(function(val,index,arr){
  console.log(arr[index]==val);  // ==> true
  sum+=val            
})
console.log(sum);          // ==> 8
Copy after login

map

The map here does not mean "map", but "mapping". [].map(); The basic usage is similar to the forEach method:

array.map(callback,[ thisObject]);
Copy after login

The parameters of callback are also similar:

[].map(function(value, index, array) {
  // ...
});
Copy after login

The function of the map method is not difficult to understand. It is "mapping", that is, the original array is "mapped" into the corresponding new array. The following example is to find the square of a numerical term:

var data=[1,3,4]

var Squares=data.map(function(val,index,arr){
  console.log(arr[index]==val);  // ==> true
  return val*val           
})
console.log(Squares);        // ==> [1, 9, 16]
Copy after login

Note: Since forEach and map are ECMA5 methods for adding new arrays, browsers below IE9 do not support it yet (dear IE). However, all the above functions can be achieved by extending the Array prototype, such as forEach Method:

if (typeof Array.prototype.forEach != "function") {
  Array.prototype.forEach = function() {
    /* 实现 */
  };
}
Copy after login

The above recommendations for forEach, $.each, and map methods in JS are all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template