jQuery.map()

Translate all items in an array or object to new array of items.

jQuery.map(array, callback)🡢 Array

array ArrayLikeObjectThe Array or an Array-like object to translate.
callback FunctionThe function to process each item against. The first argument to the function is the array item, the second argument is the index in array The function can return any value. A returned array will be flattened into the resulting array. Within the function, this refers to the global (window) object.

jQuery.map(object, callback)🡢 Array

object ObjectThe non-Array-like Object to translate.
callback FunctionThe function to process each item against. The first argument to the function is the value; the second argument is the key of the 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.

If you wish to process a jQuery object — for example, $('div').map( callback ); — use .map() instead.

The $.map() method applies a function to each item in an array or object and maps the results into a new array. Prior to jQuery 1.6, $.map() supports traversing arrays and array-like objects 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 — may be passed to $.map().

// The following object is array-like.
var fakeArray = { length: 2, 0: "Addy", 1: "Subtracty" };

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

Use $.map() to change the values of an array.

JS
<div></div>
<p></p>
<span></span>
CSS
div {
  color: blue;
}
p {
  color: green;
  margin: 0;
}
span {
  color: red;
}
HTML
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(", "));
DEMO

Map the original array to a new one and add 4 to each value.

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

Map the original array to a new one, adding 1 to each value if it is bigger then zero and removing it if not.

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

Map the original array to a new one; each element is added with its original value and the value plus one.

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

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

HTML
var dimensions = { width: 10, height: 15, length: 20 };
dimensions = $.map(dimensions, function (value, index) {
  return value * 2;
});
DEMO

Map an object's keys to an array.

HTML
var dimensions = { width: 10, height: 15, length: 20 };
var keys = $.map(dimensions, function (value, key) {
  return key;
});
DEMO

Map the original array to a new one; each element is squared.

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

Map the original array to a new one, removing numbers less than 50 by returning null and subtracting 45 from the rest.

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

Augment the resulting array by returning an array inside the function.

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

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)