jQuery jQuery.map() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Utility transform

What You’ll Learn

The jQuery.map() utility transforms arrays, array-like objects, and plain objects into a new array of results. This tutorial covers syntax, five worked examples, how to filter with null, flatten nested arrays, and how $.map differs from $.each and DOM .map().

01

Syntax

$.map(col, fn)

02

Transform

Return new values

03

Filter

Return null

04

Flatten

Return nested arrays

05

Objects

Map property values

06

New array

Original unchanged

Introduction

Mapping is one of the most useful patterns in JavaScript: take a list of items, run a function on each one, and collect the outputs into a new list. jQuery provides jQuery.map() (also written $.map()) as a static utility that does exactly that for arrays, array-like objects, and plain objects.

Unlike jQuery.each(), which is built for side effects and returns the original collection, $.map() is built for transformation and always returns a fresh array. If you need to map over DOM elements inside a jQuery selection, use the separate instance method $(selector).map() instead.

Understanding the jQuery.map() Method

jQuery.map(collection, callback) invokes your callback once for every top-level item. For arrays and array-like objects, the callback receives (value, index). For plain objects, it receives (value, key). Each return value is collected; null and undefined are skipped; returned arrays are flattened one level into the result.

Choose $.map() when you want a transformed copy of your data — doubling numbers, formatting strings, extracting object values, or building a filtered list — without mutating the source collection.

💡
Beginner Tip

Watch the callback argument order: $.map passes (value, index) on arrays, but $.each passes (index, value). Swapping them is a common copy-paste mistake.

📝 Syntax

General form of jQuery.map:

jQuery
jQuery.map( arrayOrObject, callback(valueOrElement, indexOrKey) )
// or
$.map( arrayOrObject, callback(valueOrElement, indexOrKey) )

Parameters

  • arrayOrObject — an array, array-like object (with length and indexed values), or plain JavaScript object to translate.
  • callback — a function invoked for each item. For arrays: (element, index). For objects: (value, key). Inside the callback, this refers to the global object (window in browsers).

Return value

  • A new array containing the mapped results.
  • Items where the callback returns null or undefined are omitted.
  • Arrays returned from the callback are concatenated (flattened one level) into the result.

Minimal workflow

jQuery
var doubled = $.map([1, 2, 3], function (n) {
  return n * 2;
});

console.log(doubled); // [2, 4, 6]

⚡ Quick Reference

GoalCode
Transform an array$.map(arr, function (val, i) { return val * 2; })
Map object values$.map(obj, function (val, key) { ... })
Remove an itemReturn null or undefined
Flatten one levelReturn an array from the callback
Mutates original?No — returns a new array
Array callback order(value, index) — not (index, value)

📋 $.map vs $.each vs $(sel).map

Three similarly named tools — different jobs and return values.

$.map
new array

Static utility; transform any collection

$.each
side effects

Iterate; returns original collection

$(sel).map
DOM only

Instance method on jQuery selections

Arg order
val, index

$.each uses index, value instead

Examples Gallery

Each example uses $.map(). Open DevTools or use the Try-it links to run them interactively.

📚 Getting Started

Transform arrays into new arrays with a callback return value.

Example 1 — Add 4 to Each Number

Map a numeric array by returning n + 4 for every element — the classic intro from the jQuery API docs.

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

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

How It Works

jQuery calls your function once per element. Each return value is pushed into a new array. The original [0, 1, 2] is never modified.

📈 Practical Patterns

Formatting, filtering, flattening, and object mapping.

Example 2 — Uppercase Letters with Index Suffix

Combine each letter with its index — callback receives (value, index).

jQuery
var letters = ["a", "b", "c", "d", "e"];

var formatted = $.map(letters, function (n, i) {
  return n.toUpperCase() + i;
});

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

How It Works

The second parameter i is the zero-based index. This mirrors the official jQuery demo that uppercases each letter and appends its position.

Example 3 — Filter by Returning null

Keep only values greater than zero and add 1; return null to drop the rest.

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

When the callback returns null or undefined, jQuery skips that slot entirely. This gives you map-and-filter in one pass without a separate $.grep call.

Example 4 — Flatten by Returning Nested Arrays

Return [n, n + 1] from each item; jQuery merges the inner arrays one level deep.

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

Array return values are concatenated into the result. Three source items each produce two outputs, yielding six elements. This one-level flatten predates Array.flatMap and is still handy in legacy jQuery codebases.

Example 5 — Map Object Values to a New Array

Double every numeric property on a dimensions object — object iteration added in jQuery 1.6.

jQuery
var dimensions = { width: 10, height: 15, length: 20 };

var doubled = $.map(dimensions, function (value, key) {
  return value * 2;
});

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

How It Works

On plain objects, the callback receives (value, key). The result is always an array of values — keys are not preserved. To keep keys, build an object manually or use a different pattern.

🚀 Common Use Cases

  • Data normalization — convert raw API rows into display strings or numeric scores.
  • Inline filter-map — transform valid items and drop invalid ones with null in one loop.
  • Object value extraction — pull and transform enumerable properties into an array for charts or templates.
  • Building option lists — map id/name pairs from server data into label strings for a <select>.
  • Legacy flattening — expand each item into multiple outputs by returning nested arrays.
  • Array-like objects — map over NodeLists or custom collections that expose length and indexed slots.

🧠 How jQuery.map() Builds the Result

1

Detect type

jQuery checks whether the input is array-like (numeric length) or a plain object.

Inspect
2

Invoke callback

For each item, call your function with (value, index) or (value, key).

Transform
3

Collect return

Skip null/undefined; flatten returned arrays one level; append everything else.

Merge
4

Return new array

The original collection is untouched — you get a fresh result array.

📝 Notes

  • Arrays and array-like objects supported since jQuery 1.0; plain objects since jQuery 1.6.
  • Do not use $.map on a jQuery DOM object — call $(sel).map(fn) instead.
  • Inside the callback, this is the global object, not the current array element.
  • Mapping an object always yields an array of values; property keys are not included unless you return them yourself.
  • Only one level of array flattening happens automatically — deeply nested arrays stay nested.
  • For side effects without collecting values, prefer $.each; for immutable transforms, prefer $.map.

Browser Support

jQuery.map() has been part of jQuery since jQuery 1.0+ for arrays (objects since 1.6). It works wherever jQuery runs — all modern browsers and legacy IE when paired with a supported jQuery build.

jQuery 1.0+

jQuery jQuery.map()

Supported in jQuery 1.x, 2.x, and 3.x. Behavior is consistent across browsers because jQuery owns the mapping implementation.

100% With jQuery loaded
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
jQuery.map() Universal

Bottom line: Safe in any project that already includes jQuery. Pair with jQuery 3.7+ for the latest utility behavior and security patches.

Conclusion

The jQuery.map() utility is jQuery’s answer to “give me a new array based on this data.” Return transformed values, drop items with null, or flatten nested arrays — all without touching the original collection.

Practice the five examples above until the callback argument order and return rules feel natural, then explore jQuery.grep() when you need to filter without transforming values.

💡 Best Practices

✅ Do

  • Use $.map when you need a transformed copy as an array
  • Return null to filter items in the same pass
  • Remember array callbacks use (value, index) order
  • Use $(sel).map() when mapping DOM elements in a selection
  • Prefer native Array.map when jQuery is not loaded and data is a plain array

❌ Don’t

  • Confuse $.map with $.each argument order
  • Call $.map($("div"), fn) — use $("div").map(fn)
  • Expect object keys to survive mapping automatically
  • Mutate the source array when immutability is required
  • Assume this inside the callback is the current element

Key Takeaways

Knowledge Unlocked

Five things to remember about jQuery.map()

Transform any collection into a new array the jQuery way.

5
Core concepts
02

null skip

Filter inline

Filter
📑 03

Flatten

Return arrays

Merge
🔄 04

val, index

Not like $.each

Order
05

Not DOM

Use $(sel).map

Pitfall

❓ 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. Same name, different targets — check whether your first argument is data or a jQuery object.
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 (NodeLists, arguments), 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.
Did you know?

Before jQuery 1.6, $.map() only worked on arrays and array-like objects. Object support arrived in 1.6 — the same release that expanded many core utilities. Returning an array from the callback to flatten results has worked since the beginning.

Continue to jQuery.grep()

Learn how to filter array elements with a boolean test and optional invert flag.

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.

6 people found this page helpful