1 line
5.2 KiB
JSON
1 line
5.2 KiB
JSON
{"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, <code>this</code> 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, <code>this</code> 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<div></div>\n<p></p>\n<span></span>\n"},{"desc":"Map the original array to a new one and add 4 to each value.","code":"\n$.map( [ 0, 1, 2 ], function( n ) {\n return n + 4;\n});\n","results":"\n[4, 5, 6]\n"},{"desc":"Map the original array to a new one, adding 1 to each value if it is bigger then zero and removing it if not.","code":"\n$.map( [ 0, 1, 2 ], function( n ) {\n return n > 0 ? n + 1 : null;\n});\n","results":"\n[ 2, 3 ]\n"},{"desc":"Map the original array to a new one; each element is added with its original value and the value plus one.","code":"\n$.map( [ 0, 1, 2 ], function( n ) {\n return [ n, n + 1 ];\n});\n","results":"\n[ 0, 1, 1, 2, 2, 3 ]\n"},{"desc":"Map the original object to a new array and double each value.","code":"\nvar dimensions = { width: 10, height: 15, length: 20 };\ndimensions = $.map( dimensions, function( value, index ) {\n return value * 2;\n});\n","results":"\n[ 20, 30, 40 ]\n"},{"desc":"Map an object's keys to an array.","code":"\nvar dimensions = { width: 10, height: 15, length: 20 };\nvar keys = $.map( dimensions, function( value, key ) {\n return key;\n});\n","results":"\n[ \"width\", \"height\", \"length\" ]\n"},{"desc":"Map the original array to a new one; each element is squared.","code":"\n$.map( [ 0, 1, 2, 3 ], function( a ) {\n return a * a;\n});\n","results":"\n[ 0, 1, 4, 9 ]\n"},{"desc":"Map the original array to a new one, removing numbers less than 50 by returning <code>null</code> 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 <p>If you wish to process a jQuery object — for example, <code>$('div').map( callback );</code> — use <a href=\"/map/\">.map()</a> instead. </p>\n <p>The <code>$.map()</code> method applies a function to each item in an array or object and maps the results into a new array. <strong>Prior to jQuery 1.6</strong>, <code>$.map()</code> supports traversing <em>arrays only</em>. <strong>As of jQuery 1.6</strong> it also traverses objects.</p>\n <p>Array-like objects — those with a <code>.length</code> property <em>and</em> a value on the <code>.length - 1</code> index — must be converted to actual arrays before being passed to <code>$.map()</code>. The jQuery library provides <a href=\"/jQuery.makeArray/\">$.makeArray()</a> for such conversions.</p>\n <pre><code>\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 </code></pre>\n <p>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.</p>\n <p>The function can return:</p>\n <ul>\n <li>the translated value, which will be mapped to the resulting array</li>\n <li><code>null</code> or <code>undefined</code>, to remove the item</li>\n <li>an array of values, which will be flattened into the full array</li>\n </ul>\n "}]} |