jQuery source code parsing (vi) the difference between the $ .each and $ .map

$ .Each mainly used to iterate or objects, for example:

var arr = [11,12,13,14 ]; 
$ .each (arr, function (element, index) {     // iterate array arr 
    the console.log (element, index)             // print element and index 
})

Output is as follows:

Although the $ .map also iterate, but it can generate an array, you can simply return a value in the function, as follows:

var arr = [11,12,13,14 ]; 
 var B = $ .map (arr, function (Element, index) {     // iterate array arr 
    IF (Element% 2 == 0) return Element                     // return only can figure 2 is divisible

writer by: Desert QQ: 22969969

})
console.log(b)

writer by: Desert QQ: 22969969

Output:

Summary: . $ .Each is used to iterate, $ map in addition to iterate, filtered, and can also generate a new array, of course, not necessarily the filter, any logic can be done in the function in the map, as long as the return value can meet the requirements of

Guess you like

Origin www.cnblogs.com/greatdesert/p/11429021.html