javascript: definition and usage of map ()

The use of map and forEach are very similar:

forEach:

var array = ['a', 'b', 'c'];

array.forEach(function(element) {
  console.log(element);
});

The output is:
a;
b;
c;

map:

var users = [
	    {name: "熊大", "email": "[email protected]"},
	    {name: "熊二",   "email": "[email protected]"},
	    {name: "光头强",  "email": "[email protected]"}
	];
	// emails => email的数组
	var emails = users.map(function (user) { return user.email; });
	console.log(emails);

The output is:
[zhang @ email.com, jiang @ email.com, li @ email.com]

The map () method returns a new array. The elements in the array are the values ​​of the original array elements after the function is called.
The map () method processes the elements in order according to the order of the original array elements. Note: map () does not detect empty arrays. Note: map () returns a new array, map () does not change the original array. Syntax: array.map (function (currentValue, index, arr), thisValue) Parameter description: function (currentValue, index, arr): required. Function, every element in the array will execute this function Function parameters: must. The value of the current element is optional. The index value of the current element is optional. Array object to which the current element belongs : optional. The object is used as the execution callback, passed to the function, and used as the "this" value. If thisValue is omitted, or null or undefined is passed in, then this of the callback function is the global object. Note : The function (currentValue, index, arr) callback function must return the return value, otherwise it will return an undefind array








currentValue
index
arr
thisValue



Published 28 original articles · praised 7 · visits 1173

Guess you like

Origin blog.csdn.net/haduwi/article/details/105363682