Home >Web Front-end >JS Tutorial >Usage examples of $.map() in jquery (code)

Usage examples of $.map() in jquery (code)

不言
不言Original
2018-08-16 11:17:271976browse

The content of this article is about the usage examples (code) of $.map in jquery. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

$.map(data,function(item,index){return XXX})

Traverse each element in the data array and form a Put the new elements into the returned array

var b = $.map( [55,1,2], function( item,index ) {  return {  "label": item,  "value": index  }});
                        
                        alert(b[0].label +"  "+ b[0].value);

The output is: 55 0

[55,1,2] is an array ,According to the return condition, when the item in the function is 55, the index, which is the subscript of the array, is 0

$.map() The brackets in the brackets are equivalent to a loop

Loop multiple pieces of data and define the data as b

var array = [0, 1, 52, 97];
array = $.map(array, function(a, index) {  
return [a - 45, index];
});

The output is: [-45, 0, -44, 1, 7, 2, 52, 3]

Related recommendations:

jQuery method of traversing multiple maps in json_jquery

jQuery map( )Method usage example_jquery

The above is the detailed content of Usage examples of $.map() in jquery (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn