There is a map method in JavaScript, which is used to return a new array and process the elements in sequence according to the order of the original array elements; the map syntax is "array.map(function(currentValue,index,arr), thisValue) ".
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
Does javascript have map?
There is a map method in javascript.
JavaScript Array map() method definition and usage
The map() method returns a new array, and the elements in the array are the values of the original array elements after calling the function.
The map() method processes elements sequentially in the order of the original array elements.
Note: map() will not detect empty arrays. map() does not change the original array.
Syntax
array.map(function(currentValue,index,arr), thisValue)
Parameter description
Parameter function(currentValue, index,arr) must: function, each element in the array will execute this Function
Parameter currentValue Mandatory: the value of the current element
index Optional: the index value of the current element
arr Optional: the array object to which the current element belongs
thisValue Optional: The object is used as the execution callback, passed to the function, and used as the value of "this".
If thisValue is omitted, or null or undefined is passed in, then the this of the callback function is the global object.
Return value: Returns a new array. The elements in the array are the values of the original array elements after calling the function.
Example
Each element in the array is multiplied by the value specified in the input box, and a new array is returned:
var numbers = [65, 44, 12, 4]; function multiplyArrayElement(num) { return num * document.getElementById("multiplyWith").value; } function myFunction() { document.getElementById("demo").innerHTML = numbers.map(multiplyArrayElement); }
Running effect:
Recommended study: "javascript basic tutorial"
The above is the detailed content of Does javascript have a map?. For more information, please follow other related articles on the PHP Chinese website!