English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
map()The method creates a new array and calls the provided function for each element in the array.
map()The method executes the callback function once for each array index.
Note : The map() method does not change the original array.
array.map(callback, thisArg)
var nums1 = [1 5 20, 14 55 16] var nums2 = nums1.map(twice); function twice(element) { return (element * 2); }Test see‹/›
The numbers in the table specify the first browser version that fully supports the map() method:
Method | |||||
map() | Is | 1.5 | Is | Is | 9 |
Parameter | Description |
---|---|
callback | A function that is run for each element in the array. Function Parameters:
|
thisArg | (Optional) Executecallbackis used asThisObject |
Return Value: | A new array where each element is the result of a callback function |
---|---|
JavaScript Version: | ECMAScript 5 |
This example uses a number array and creates a new array containing the square roots of the numbers in the array:
var nums1 = [4 9 16 25] var nums2 = nums1.map(Math.sqrt);Test see‹/›