.map() transforms a jQuery collection into a new jQuery object by applying a callback to each element and collecting the returned values. It is the jQuery equivalent of JavaScript's Array.prototype.map(), but operates on DOM elements and returns a jQuery object that you can further manipulate or convert to a plain array.
Extracting Data
The most common use of .map() is extracting a property or data value from each element in a set — for example, collecting all input values, all data-id attributes, or all text contents into an array.
Converting to Array
Chain .get() (or .toArray()) after .map() to convert the resulting jQuery object to a plain JavaScript array, ready for use with standard array methods like Array.join() or JSON.stringify().
- Callback signature:
function(index, element) - Return
nullorundefinedto exclude an item from the result - Chain
.get()to get a plain array $.map(array, fn)— utility version for plain arrays
Use $.map() (the utility version) when transforming a plain JavaScript array or object rather than a jQuery set — it avoids the overhead of creating a jQuery object.