jQuery API

jQuery.map()

jQuery.map( array, callback(elementOfArray, indexInArray) ) Returns: Array

Description: Translate all items in an array or array-like object to another array of items.

  • version added: 1.0jQuery.map( array, callback(elementOfArray, indexInArray) )

    arrayThe Array to translate.

    callback(elementOfArray, indexInArray)The function to process each item against. The first argument to the function is the list item, the second argument is the index in array The function can return any value. this will be the global window object.

The $.map() method applies a function to each item in an array, collecting the results into a new array.

The translation function that is provided to this method is called for each item in the array and is passed two arguments: The item to be translated, and the index within the array.

The function can return:

  • the translated value, which will be mapped to the resulting array
  • null, to remove the item
  • an array of values, which will be flattened into the full array

Map can iterate through Array-like objects, like a jQuery object, that have a length property.

Examples:

Example: A couple examples of using .map()

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  p { color:green; margin:0; }
  span { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	<div></div>
  <p></p>
  <span></span>
  
<script>

    var arr = [ "a", "b", "c", "d", "e" ];
    $("div").text(arr.join(", "));

    arr = jQuery.map(arr, function(n, i){
      return (n.toUpperCase() + i);
    });
    $("p").text(arr.join(", "));

    arr = jQuery.map(arr, function (a) { return a + a; });
    $("span").text(arr.join(", "));

</script>
</body>
</html>

Demo:

Example: Maps the original array to a new one and adds 4 to each value.

$.map( [0,1,2], function(n){
   return n + 4;
 });

Result:

[4, 5, 6] 

Example: Maps the original array to a new one and adds 1 to each value if it is bigger then zero, otherwise it's removed.

$.map( [0,1,2], function(n){
   return n > 0 ? n + 1 : null;
 });

Result:

[2, 3] 

Example: Maps the original array to a new one, each element is added with it's original value and the value plus one.

$.map( [0,1,2], function(n){
   return [ n, n + 1 ];
 });

Result:

[0, 1, 1, 2, 2, 3] 

Example: Maps the original array to a new one, each element is squared.

$.map( [0,1,2,3], function (a) { return a * a; } );

Result:

[0, 1, 4, 9] 

Example: Remove items by returning null from the function. This removes any numbers less than 50, and the rest are decreased by 45.

$.map( [0, 1, 52, 97], function (a) { return (a > 50 ? a - 45 : null); } );

Result:

[7, 52] 

Example: Augmenting the resulting array by returning an array inside the function.

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

Result:

[-45, 0, -44, 1, 7, 2, 52, 3] 

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for jQuery.map() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • ratbeard
    I filed a bug and patch about this a while back about the binding of this function, which was rejected. The current binding: "[this] will be the global window object." violates the principle of least surprise, for no added benefit.

    The other iteration methods, both on jQuery and on jQuery.prototype set [this] to be the current iteration's element. I can see not wanting to change an API that could break compatibility, but any code that relies on the current [window] scope behavior probably doesn't exist, or is working by coincidence.