{"name":"jQuery.map","type":"method","title":"jQuery.map()","deprecated":null,"removed":null,"desc":"Translate all items in an array or object to new array of items.","categories":["utilities","version/1.0","version/1.6"],"entries":[{"return":"Array","signatures":[{"added":"1.0","argument":[{"desc":"The Array to translate.","name":"array","type":"Array"},{"desc":"The function to process each item against. The first argument to the function is the array item, the second argument is the index in array The function can return any value. Within the function, this refers to the global (window) object.","name":"callback( elementOfArray, indexInArray )","type":"Function"}]},{"added":"1.6","argument":[{"type":[{"name":"Array"},{"name":"Object"}],"desc":"The Array or Object to translate.","name":"arrayOrObject"},{"desc":"The function to process each item against. The first argument to the function is the value; the second argument is the index or key of the array or object property. The function can return any value to add to the array. A returned array will be flattened into the resulting array. Within the function, this refers to the global (window) object. ","name":"callback( value, indexOrKey )","type":"Function"}]}],"examples":[{"desc":"Use $.map() to change the values of an array.","code":"\nvar arr = [ \"a\", \"b\", \"c\", \"d\", \"e\" ];\n$( \"div\" ).text( arr.join( \", \" ) );\n\narr = jQuery.map( arr, function( n, i ) {\n return ( n.toUpperCase() + i );\n});\n$( \"p\" ).text( arr.join( \", \" ) );\n\narr = jQuery.map( arr, function( a ) {\n return a + a;\n});\n$( \"span\" ).text( arr.join( \", \" ) );\n","css":"\n div {\n color: blue;\n }\n p {\n color: green;\n margin: 0;\n }\n span {\n color: red;\n }\n","html":"\n
null and subtracting 45 from the rest.","code":"\n$.map( [ 0, 1, 52, 97 ], function( a ) {\n return (a > 50 ? a - 45 : null);\n});\n","results":"\n[ 7, 52 ]\n"},{"desc":"Augment the resulting array by returning an array inside the function.","code":"\nvar array = [ 0, 1, 52, 97 ];\narray = $.map( array, function( a, index ) {\n return [ a - 45, index ];\n});\n","results":"\n[ -45, 0, -44, 1, 7, 2, 52, 3]\n"}],"longdesc":"\n If you wish to process a jQuery object — for example, $('div').map( callback ); — use .map() instead.
The $.map() method applies a function to each item in an array or object and maps the results into a new array. Prior to jQuery 1.6, $.map() supports traversing arrays only. As of jQuery 1.6 it also traverses objects.
Array-like objects — those with a .length property and a value on the .length - 1 index — must be converted to actual arrays before being passed to $.map(). The jQuery library provides $.makeArray() for such conversions.
\n// The following object masquerades as an array.\nvar fakeArray = { \"length\": 2, 0: \"Addy\", 1: \"Subtracty\" };\n\n// Therefore, convert it to a real array\nvar realArray = $.makeArray( fakeArray )\n\n// Now it can be used reliably with $.map()\n$.map( realArray, function( val, i ) {\n // Do something\n});\n \n The translation function that is provided to this method is called for each top-level element in the array or object and is passed two arguments: The element's value and its index or key within the array or object.
\nThe function can return:
\nnull or undefined, to remove the item