jQuery.map( array, callback(elementOfArray, indexInArray) ) Returns: Array
Description: Translate all items in an array or object to new array of items.
-
version added: 1.0jQuery.map( array, callback(elementOfArray, indexInArray) )
-
arrayType: ArrayThe Array to translate.
-
callback(elementOfArray, indexInArray)Type: Function()The function to process each item against. The first argument to the function is the array item, the second argument is the index in array The function can return any value. Within the function,
this
refers to the global (window) object.
-
-
version added: 1.6jQuery.map( arrayOrObject, callback( value, indexOrKey ) )
-
arrayOrObjectThe Array or Object to translate.
-
callback( value, indexOrKey )Type: Function()The function to process each item against. The first argument to the function is the value; the second argument is the index or key of the array or object property. The function can return any value to add to the array. A returned array will be flattened into the resulting array. Within the function,
this
refers to the global (window) object.
-
If you wish to process a jQuery object — for example, $('div').map( callback );
— use .map() instead.
The $.map()
method applies a function to each item in an array or object and maps the results into a new array. Prior to jQuery 1.6, $.map()
supports traversing arrays only. As of jQuery 1.6 it also traverses objects.
Array-like objects — those with a .length
property and a value on the .length - 1
index — must be converted to actual arrays before being passed to $.map()
. The jQuery library provides $.makeArray() for such conversions.
1
2
3
4
5
6
7
8
9
10
|
|
The translation function that is provided to this method is called for each top-level element in the array or object and is passed two arguments: The element's value and its index or key within the array or object.
The function can return:
- the translated value, which will be mapped to the resulting array
null
orundefined
, to remove the item- an array of values, which will be flattened into the full array
Examples:
Example: Use $.map() to change the values of an array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
|
Example: Map the original array to a new one and add 4 to each value.
1
2
3
|
|
Result:
1
|
|
Example: Map the original array to a new one, adding 1 to each value if it is bigger then zero and removing it if not.
1
2
3
|
|
Result:
1
|
|
Example: Map the original array to a new one; each element is added with its original value and the value plus one.
1
2
3
|
|
Result:
1
|
|
Example: Map the original object to a new array and double each value.
1
2
3
4
|
|
Result:
1
|
|
Example: Map an object's keys to an array.
1
2
3
4
|
|
Result:
1
|
|
Example: Map the original array to a new one; each element is squared.
1
2
3
|
|
Result:
1
|
|
Example: Map the original array to a new one, removing numbers less than 50 by returning null
and subtracting 45 from the rest.
1
2
3
|
|
Result:
1
|
|
Example: Augment the resulting array by returning an array inside the function.
1
2
3
4
|
|
Result:
1
|
|