Home  >  Article  >  Web Front-end  >  How to use the map method of javascript array

How to use the map method of javascript array

青灯夜游
青灯夜游Original
2021-07-19 18:46:158158browse

In JavaScript, the map method of an array is used to call the specified callback function for each element of the array and return an array containing the results; the syntax format is "array.map(callbackfn[, thisArg]); ".

How to use the map method of javascript array

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The JavaScript map() method can call the specified callback function for each element of the array and return an array containing the results. The specific usage is as follows:

array.map(callbackfn[, thisArg]);

Parameter description:

  • array: required parameter, an array object.

  • callbackfn: required parameter, a function that can receive up to three parameters. For each element in the array, the map() method calls the callbackfn function once.

  • thisArg: Optional parameter, an object that can be referenced by the this keyword in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

The map() method returns a new array where each element is the callback function return value of the associated original array element. The map() method calls the callbackfn function once for each element in the array (in ascending index order) and does not call the callback function for missing elements in the array.

In addition to array objects, the map() method can be used by any object with a length property and an indexed property name, such as an Arguments parameter object.

The syntax of the callback function is as follows:

function callbackfn (value, index, array);

Users can use up to three parameters to declare the callback function. The parameter description of the callback function is as follows:

  • #value: The value of the array element.

  • index: Numeric index of the array element.

  • array: Array object containing the element.

The map() method does not directly modify the original array, but the callback function may modify it. The results obtained by modifying the array object after the map method is started are shown in the table.

The impact of the callback function modifying the array
Conditions after the map method is started Whether the element is passed to the callback function
Add elements beyond the original length of the array No
Add elements to fill missing elements in the array Yes, if the index has not been passed to the callback function
The element has changed Yes, if the element has not been passed to the callback function
Remove element from array No unless the element has been passed to the callback function

Example 1

The following example uses the map() method to map an array, square the value of each element in the array, multiply it by the PI value, use the area value of the returned circle as the element value of the new array, and finally return the new array.

function f (radius) {
    var area = Math.PI * (radius * radius);
    return area.toFixed(0);
}
var a = [10,20,30];
var a1 = a.map(f);
console.log(a1);

Example 2

The following example uses the map() method to map an array, divide the value of each element in the array by a threshold, and then return the new array where Both the callback function and the threshold exist as properties of the object. This method demonstrates how to use the thisArg parameter in the map.

var obj = {
    val : 10,
    f : function (value) {
        return value % this.val;
    }
}
var a = [6,12,25,30];
var a1 = a.map(obj.f, obj);
console.log(a1);  //6,2,5,0

Example 3

The following example demonstrates how to use JavaScript built-in methods as callback functions.

var a = [9, 16];
var a1 = a.map(Math.sqrt);
console.log(a1);  //3,4

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to use the map method of javascript array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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