jQuery map() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is jQuery.map()?

jQuery.map(arrayOrObject, callback) (alias $.map) translates every item in an array, array-like object, or plain object into a new array of results (arrays since 1.0; plain objects since 1.6).

Remember
$.map(array, function (value, index) { … })  → Array
$.map(object, function (value, key) { … })   → Array

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

// return null / undefined → skip item
// return [a, b]           → flatten one level
NeedUse
Transform data → new array$.map(data, fn)
Side effects only$.each(data, fn)
Map DOM elements$(sel).map(fn)

How to Use jQuery.map()

Pass a collection and a callback that returns the mapped value.

  1. Start with an array, array-like object, or plain object.
  2. Write a callback: arrays get (value, index); objects get (value, key).
  3. Return a value to keep it, null/undefined to skip, or an array to flatten one level.
  4. Use the new array result — the original collection is not replaced.
PieceRequired?Role
array / objectYesSource to translate
callbackYesMaps each item → result value(s)
Return—New Array

Pseudocode

Pseudocode
function map(collection, callback):
    result = []
    for each (value, indexOrKey) in collection:
        mapped = callback(value, indexOrKey)
        if mapped is null or undefined:
            continue                  // skip
        if mapped is an array:
            append each item to result   // flatten 1 level
        else:
            append mapped to result
    return result

// Callback this → window (global), not the item

Cheat Sheet

Keep these facts handy while transforming collections.

TopicDetail
SyntaxjQuery.map(collection, callback)
ArraysSince jQuery 1.0
ObjectsSince jQuery 1.6
Callback (array)(value, index)
Callback (object)(value, key)
Skip itemReturn null / undefined
FlattenReturn an array → one level

Live Preview

Run official-style $.map transforms on small sample data.

Browser demo of $.map() — same idea as the examples below.

Result
$.map([0, 1, 2], n => n + 4)
→ [4, 5, 6]

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Transform Each Value

Add 4 to every number in an array.

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

console.log(result); // [4, 5, 6]
Try It Yourself

How It Works

  1. Source. Start with [0, 1, 2].
  2. Callback. For each n, return n + 4.
  3. Collect. jQuery pushes each return value into a new array.
  4. Result. You get [4, 5, 6] — original array unchanged.

Example 2 — Use Value and Index

Official demo idea: uppercase each letter and append its index.

jQuery
var formatted = $.map(["a", "b", "c", "d", "e"], function (n, i) {
  return n.toUpperCase() + i;
});

console.log(formatted.join(", ")); // "A0, B1, C2, D3, E4"
Try It Yourself

How It Works

  1. Args. Callback receives (value, index) — value first.
  2. Transform. toUpperCase() plus index builds "A0", "B1", …
  3. Join. Display as a comma-separated string.
  4. Trap. Do not confuse with $.each, which uses (index, value).

Example 3 — Skip Items with null

Drop zeros; keep and increment positive numbers.

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

console.log(filtered); // [2, 3]
Try It Yourself

How It Works

  1. Branch. Positive values return n + 1; zero returns null.
  2. Skip. null / undefined are omitted from the result.
  3. Keep. 1 → 2 and 2 → 3 remain.
  4. Filter tip. For filter-only jobs, also see $.grep().

Example 4 — Flatten One Level

Return an array from the callback; jQuery concatenates it into the result.

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

console.log(flat); // [0, 1, 1, 2, 2, 3]
Try It Yourself

How It Works

  1. Return arrays. Each call yields [n, n + 1].
  2. Flatten. jQuery appends those items one level into the result.
  3. Expand. Three inputs become six numbers.
  4. vs Array.map. Native Array.map would keep nested arrays instead.

Example 5 — Map a Plain Object

Double every numeric property (object support since jQuery 1.6).

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

console.log(doubled); // [20, 30, 40]
Try It Yourself

How It Works

  1. Object source. Properties are walked as (value, key).
  2. Transform. Each value is doubled.
  3. Array out. Result is always an array of mapped values — not an object.
  4. Keys tip. Return key instead of value if you need property names.

Edge Cases & Pitfalls

Check these before mixing $.map with $.each or DOM mapping.

order

Callback args: value first

$.map uses (value, index). $.each uses (index, value). Swapping them is a common bug.

dom

jQuery collections → use .map()

Official guidance: map DOM selections with $(sel).map(), not the static utility.

this

this is the global object

Inside the callback, this is window, not the current item. Use the value argument instead.

flatten

Returned arrays flatten one level only

Deeper nesting stays nested. Plan for a single flatten level when returning arrays from the callback.

Browser Support

jQuery.map() needs jQuery in the browser. Arrays/array-likes since jQuery 1.0+; plain objects since jQuery 1.6+. Works in all browsers supported by your jQuery build.

✓ jQuery 1.0+ · Objects 1.6+

jQuery.map()

Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery loaded. Transform arrays, array-likes, and (since 1.6) plain objects.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$.map() Browsers

Bottom line: Use $.map(collection, callback) to build a new array from data — use .map() for DOM collections.

Performance Notes

ApproachCostBest for
$.map(arr, fn)O(n)Transform + optional skip/flatten
$.each(arr, fn)O(n)Side effects, no new array
arr.map(fn)O(n)Native arrays only

Prefer $.map when you need the new array (and null-skip / flatten). Prefer $.each when you only need to loop.

Key Takeaways

  • Role: $.map builds a new array from callback returns.
  • Args: (value, index|key) — value first.
  • Extras: null skips; arrays flatten one level.
  • DOM: use $(sel).map() for element collections.

One line: $.map(collection, fn) translates data into a new array — skip with null, flatten returned arrays, and remember value comes first.

Frequently Asked Questions

jQuery.map(collection, callback) walks every item in an array, array-like object, or plain object and builds a new array from the values your callback returns. It is a transform utility — not tied to DOM selections.
jQuery.each() runs side effects and returns the original collection. jQuery.map() collects callback return values into a brand-new array. Also note the callback argument order differs: $.map uses (value, index) for arrays, while $.each uses (index, value).
jQuery.map(array, fn) is a static utility for any data. $(selector).map(fn) is an instance method that maps over elements inside a jQuery DOM collection. Official docs: to process a jQuery object, use .map() instead.
Any value to include in the result array; null or undefined to skip the item; or an array of values, which jQuery flattens one level into the final result.
No. It always returns a new array. Your callback could mutate nested objects if you choose, but the top-level collection reference stays the same.
Use jQuery.map() when you need to map plain objects, array-like objects, or when you want null/undefined filtering and one-level flattening built in. Array.map() only works on arrays and does not flatten nested arrays from the callback.

Next: grep()

Filter array items that pass a test function with jQuery.grep().

grep() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful