Map
Here is map not the meaning of "map", but refers to "mapping". The [].map(); basic usage is similar to the forEach method:
Array.map (callback,[thisobject]);
callbackThe parameters are similar:
[].map (function (value, index, array) { //...});
mapThe function of the method is not difficult to understand, "mapping", that is, the original array is "mapped" into a corresponding new array. The following example is the value of the square:
var data = [1, 2, 3, 4];var arrayofsquares = Data.map (function (item) { //1, 4, 9,
callbackNeed to have a return value, if not, just like this:
var data = [1, 2, 3, 4];var arrayofsquares = Data.map (function () {}); Arrayofsquares.foreach (Console.log);
As a result, as you can see, all the items in the array are mapped undefined :
In practical use, we can make use of the map method to obtain the specific attribute values in the object array conveniently. For example, the following example is also the example of a compatible demo:
var users = [ {name: "Zhang contains rhyme", "email": "[email protected]"}, {name: "Dancing Dogs", "email": "[email protected]"}, {name: "Li Xiaolu", //[email protected], [email protected], [email protected]
Array.prototypeExtensions allow IE6-IE8 browsers to also support map methods:
if (typeof Array.prototype.map! = "function") { Array.prototype.map = function (FN, context) { var arr = [];
if (typeof fn = = = "function") { for (var k = 0, length = this.length; k < length; k++) { Arr.push (Fn.call (CO ntext, This[k], K, this)); } } return arr; };}
JS Array Map method