Latest web development tutorials

jQuery.map () method

jQuery Misc Methods jQuery Misc Methods

Examples

Use $ .map () to modify the value of an array

<Div> </ div> < p> </ p> <span> </ span> <script> $ (function () { var arr = [ "A", "b", "c", "d", "e" ]; $ ( "Div") text (arr join ( ",").);.. Arr = $ map (arr, function (n, i) { return (N toUpperCase () + i. );}); $ ( "P") text (arr join ( ","..));. Arr = $ map (arr, function (A) { return a + a;}); $ ( "span") text (arr join ( ","..));}) </ script>

try it"

Definition and Usage

$ .map () Function is used to process the specified function for each element in the array (or each property of the object), and the results package for the new array is returned.

Note: 1. Before jQuery 1.6, this function is only supported through the array; 1.6 from the start, the function also supports traverse the object.
2. map () also as a function of two arguments: first, the current iteration element or attribute value, and the second is the current array index or an object property name iterations item.
3. The function returns a value as a result of an element in the array, if the return value is null or undefined, the result will not be added to the array.


grammar

$.map( object, callback )

参数 描述
object Array/Object类型 指定的需要处理的数组或对象。
callback Function类型 指定的处理函数。


Examples

More examples

Each value in the original array plus 4
Each value in the original array plus 4, the map to the new array.

Original array value greater than 0 plus 1
Original array value greater than 0 plus 1 mapped to the new array.

1 plus the value of the original array
The original of each value in the array and the value is the result of adding 1 to the newly generated after mapping array.

The value of the original array is multiplied by 2
After the original object of each value multiplied by 2, is mapped to the newly created array.

Object key
The object key is mapped to the newly created array.

Squared value of the original array
The two parties in the original array for each value and returns the result, mapped to the new generation of the array.

Remove elements
By returning null in the handler for ways to remove the element, remove the value is less than 50, but will not remove the element value is reduced 45.

Add element
By returning array in handler mode, add the final element in the result array returned.


jQuery Misc Methods jQuery Misc Methods