In JavaScript, the map() method is used to return a new array. The elements in the array are the values of the original array elements after calling the function. The map() method will process the elements in sequence according to the order of the original array elements. Syntax "array.map(function(value, index value, array){})".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript map() method
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.
Grammar:
array.map(function(item,index,arr){})
map is an array method with one parameter, the parameter is a function, and there are 3 parameters in the function
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p>点击按钮获取数组元素的平方根。</p> <button onclick="myFunction()">点我</button> <p id="demo"></p> <script> var numbers = [4, 9, 16, 25]; function myFunction() { x = document.getElementById("demo") x.innerHTML = numbers.map(Math.sqrt); } </script> </body> </html>
javascript advanced tutorial]
The above is the detailed content of What is the use of javascript map method?. For more information, please follow other related articles on the PHP Chinese website!