This article will introduce to you the usage of the map() method for processing array data and the difference between the map() method and the forEach statement. The content is very detailed. Let’s look at the specific content below.
First let’s take a look at the basic syntax of map()
var array = [ 数组数据 ]; array.map(回调函数);
Map is a method used for array data , a "callback function" can be executed for each element and the result returned as a new array.
In other words, you can perform any operation on each element of the array by writing the process to be executed in this function!
How to use map method to iterate an array?
The following example is an example of using the map method on an array containing numeric data
var items = [1,2,3,4,5]; var result = items.map(function( value ) { return value * 2; }); console.log( result );
The output result is as follows
In this example, the callback function is used to double each element of the array of values "1 to 5".
For this reason, we allocate an array whose result is doubled as the return value of "result", so you can see that the output results become twice as much as themselves.
Initially, it was all about using for statements or while statements to write loops, but using map is very convenient because it can be implemented with a very simple program!
There is also a reduce() method in Analysis on how to use the map() method for array processing in JavaScript that is similar to the map() method. For information about the reduce() method, please refer to this article: How to use reduce in Analysis on how to use the map() method for array processing in JavaScript
For the difference between reduce() and map() methods, please refer to this article: What is the difference between map() and reduce() in Analysis on how to use the map() method for array processing in JavaScript
map( ) and the forEach statement?
map() is similar to the forEach statement, it can operate on arrays in the same way.
Let’s look at a specific example
//forEach [1,2,3].forEach(function( value ) { console.log( value ); }); //map [1,2,3].map(function( value ) { console.log( value ); });
The output results are the same as
Just as you did in this example As you can see, the results obtained by forEach and map are the same.
So, what is the difference between them? The biggest difference is whether they have a return value.
In other words, forEach is just an execution method, and map returns the result as array data after execution.
Let’s look at the example in detail
forEach:
var result = [1,2,3].forEach(function( value ) { return value * 2; }); console.log( result );
The output result is as follows:
The return value of forEach cannot be seen.
map:
var result = [1,2,3].map(function( value ) { return value * 2; }); console.log( result );
The output result is
Array data can be returned as execution result .
map’s callback function
Before we saw that the callback function has only one parameter, but in fact you can use it to have three parameters!
数组数据.map( function( value, index, array ) { });
Value is the value of the array
index is the index number of the array
array is the array currently being processed
For example, in the following example , we will use the parameter index to double the value of the even number
var items = [1,2,3,4,5,6,7,8,9]; var result = items.map( function( value, index, array ) { if( index % 2 !== 0 ) { return value * 2; } else { return value; } }); console.log( result );
The output result is as follows
In addition, map has an error that does not match the original array Function to make any changes to the data, but if you use the third parameter array, you can also change the original data!
var items = [1,2,3,4,5,6,7,8,9]; items.map( function( value, index, array ) { array[index] = value * 2; }); console.log( items );
The output is as follows
#In the past, the "callback function" was specified as the first parameter of "map", but in fact you can specify Any "object" as second argument!
var array = [ 数组数据 ]; //指定对象为第2个参数 array.map( 回调函数, 对象 );
This makes it possible to combine specified arrays and objects for more convenient programming.
For example, in the following example, the food simple object foodList is specified as the second parameter of map.
var foodList = { '苹果': 45, '哈密瓜': 50, '猕猴桃': 60, '草莓': 40 }; 数组.map( function( value ) { }, foodList );
We specify any keyword from the object and return "price".
var foodList = { '苹果': 45, '哈密瓜': 50, '猕猴桃': 60, '草莓': 40 }; var order = ['草莓', '猕猴桃']; var result = order.map( function( value, index, array ) { return this[value]; }, foodList ); console.log( result );
The output is as follows
#In this example, you can see that the key is set in the array order, from which the key is obtained with the key owned in the object matches "price" and returns it as an array.
The above is the detailed content of Analysis on how to use the map() method for array processing in JavaScript. For more information, please follow other related articles on the PHP Chinese website!