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
Fundamentals
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.
Concept
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.
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]
Cheat Sheet
⚡ Quick Reference
Goal
Code
Transform an array
$.map(arr, function (val, i) { return val * 2; })
Map object values
$.map(obj, function (val, key) { ... })
Remove an item
Return null or undefined
Flatten one level
Return an array from the callback
Mutates original?
No — returns a new array
Array callback order
(value, index) — not (index, value)
Compare
📋 $.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
Hands-On
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]
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]
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]
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.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about jQuery.map()
Transform any collection into a new array the jQuery way.
5
Core concepts
⇄01
$.map
Returns new array
API
∅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.