tadam logo
Did you find an error in the text?
Select this with mouse and press
Ctrl + Enter
Xhtml.co.il Check Spelling
Orphus system

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.

jQuery.map( array, callback(elementOfArray, indexInArray) )

version added: 1.0
array
The Array to translate.
callback(elementOfArray, indexInArray)
Any value
The function to process each item against. The first argument to the function is the item, and the second argument is the index. The function can return any value. this will be the global window object.

jQuery.map( arrayOrObject, callback( value, indexOrKey ) )

version added: 1.6
arrayOrObject
The Array or Object to translate.
callback( value, indexOrKey )
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.

The $.map() method applies a function to each item in an array, collecting 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.

// The following object masquerades as an array.
var fakeArray = {"length": 1, 0: "Addy", 1: "Subtracty"};

// Therefore, convert it to a real array
var realArray = $.makeArray( fakeArray )

// Now it can be used reliably with $.map()
$
.map( realArray, function(val, i) {
 
// do something
});

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, to remove the item
  • an array of values, which will be flattened into the full array

Examples

Example 1

A couple examples of using .map()
    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(", "));

The output of the code above will be:

Example 1 - Full source:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
  <script>
  $(document).ready(function(){
    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>
  <style>
  div { color:blue; }
  p { color:green; margin:0; }
  span { color:red; }
  </style>
</head>
<body>
  <div></div>
  <p></p>
  <span></span>
</body>
</html>

Example 2

Maps the original array to a new one and adds 4 to each value.
$.map( [0,1,2], function(n){
  return n + 4;
});
The output of the code above will be:
[4, 5, 6]

Example 3

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;
});
The output of the code above will be:
[2, 3]

Example 4

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 ];
});
The output of the code above will be:
[0, 1, 1, 2, 2, 3]

Example 5

Maps the original array to a new one, each element is squared.
$.map( [0,1,2,3], function (a) { return a * a; } );
The output of the code above will be:
[0, 1, 4, 9]

Example 6

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); } );
The output of the code above will be:
[7, 52]

Example 7

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];
}); 
The output of the code above will be:
[-45, 0, -44, 1, 7, 2, 52, 3]

Example 8

Map the original object to a new array and double each value.

var dimensions = { width: 10, height: 15, length: 20 };
dimensions
= $.map( dimensions, function( value, index ) {
 
return value * 2;
});
The output of the code above will be:
[20, 30, 40]

Example 9

Map an object's keys to an array

var dimensions = { width: 10, height: 15, length: 20 },
    keys
= $.map( dimensions, function( value, index ) {
     
return index;
   
});
The output of the code above will be:
["width", "height", "length"]
Was this information helpful?
   

Comments