jQuery map() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

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).

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.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
CallWhat it does
array / objectSource to translate
callbackMaps each item → result value(s)
ReturnNew Array

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Start with an array, array-like object, or plain object.
Step 2Write a callback: arrays get (value, index); objects get (value, key).
Step 3Return a value to keep it, null/undefined to skip, or an array to flatten one level.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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() Method

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

grep() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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