Latest web development tutorials

JavaScript Array map () method

Array Object Reference JavaScript Array Object

Examples

Returns an array of elements in the array is the square root of the original array:

var numbers = [4, 9, 16, 25];

function myFunction () {
x = document.getElementById ( "demo")
x.innerHTML = numbers.map (Math.sqrt);
}

The output is:

2,3,4,5

try it"

Definition and Usage

map () method returns a new array, the array of elements in the array element to its original value after the function call processing.

map () method in accordance with the order of elements in the original array processing elements.

Note: map () does not detect an empty array.

Note: map () does not change the original array.


Browser Support

Figures in the table represent the first browser to support the method version number.

method
map () Yes 9 1.5 Yes Yes

grammar

array.map(function(currentValue,index,arr), thisValue)

Parameter Description

parameter description
function (currentValue, index, arr) have to. Function, each element of the array will perform this function function parameters:
parameter description
currentValue have to. The value of the current element
index Optional. The index value of the current element
arr Optional. An array of objects belonging to the current element
thisValue Optional. As the callback object use, passed to the function, it is used as "this" value.
If you omit thisValue, "this" value "undefined"

technical details

return value: It returns a new array, the array of elements in the array element to its original value after the function call processing.
JavaScript version: 1.6

More examples

Examples

Value input box specified for each element in the array multiplied, and returns a new array:

var numbers = [65, 44, 12, 4];

function multiplyArrayElement (num) {
return num * document.getElementById ( "multiplyWith" ) .value;
}

function myFunction () {
document.getElementById ( "demo") .innerHTML = numbers.map (multiplyArrayElement);
}

try it"

Array Object Reference JavaScript Array Object