Easy Tutorial
❮ Jq Event Target Html Outerheight ❯

jQuery.map() Method

jQuery Miscellaneous Methods

Example

Using $.map() to modify the values 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>

Definition and Usage

The $.map() function is used to process each element in an array (or each property in an object) with a specified function, and return the processed results as a new array.

Note: 1. Before jQuery 1.6, this function only supported traversing arrays; from 1.6 onwards, it also supports traversing objects.


Syntax

Parameter Description
object Array/Object type The array or object to be processed.
callback Function type The processing function.

More Examples

Add 4 to each value in the original array

Increment values greater than 0 in the original array

Increment each value in the original array

Double each value in the original array

Keys in the object

Square of each value in the original array

Remove elements

Add elements


jQuery Miscellaneous Methods

❮ Jq Event Target Html Outerheight ❯