1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
<!DOCTYPE html>
<html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
<div class="entry-content">
<div class="entry-title roundTop">
<h1 class="jq-clearfix">jQuery.map()</h1>
<div class="entry-meta jq-clearfix">
Categories:
<span class="category"><a href="http://api.jquery.com/category/utilities/" title="View all posts in Utilities">Utilities</a></span>
</div>
</div>
<div id="jQuery-map1" class="entry method">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">jQuery.map( array, callback(elementOfArray, indexInArray) )</span> <span class="returns">Returns: <a class="return" href="http://api.jquery.com/Types/#Array">Array</a></span>
</h2>
<div class="jq-box roundBottom entry-details">
<p class="desc"><strong>Description: </strong>Translate all items in an array or object to new array of items.</p>
<ul class="signatures">
<li class="signature" id="jQuery-map-array-callbackelementOfArray- indexInArray">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>jQuery.map( array, callback(elementOfArray, indexInArray) )</h4>
<p class="arguement"><strong>array</strong>The Array to translate.</p>
<p class="arguement"><strong>callback(elementOfArray, indexInArray)</strong>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.</p>
</li>
<li class="signature" id="jQuery-map-arrayOrObject-callback value- indexOrKey ">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.6/">1.6</a></span>jQuery.map( arrayOrObject, callback( value, indexOrKey ) )</h4>
<p class="arguement"><strong>arrayOrObject</strong>The Array or Object to translate.</p>
<p class="arguement"><strong>callback( value, indexOrKey )</strong>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. </p>
</li>
</ul>
<div class="longdesc">
<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>
<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="http://api.jquery.com/jQuery.makeArray/">$.makeArray()</a> for such conversions.</p>
<pre class="prettyprint">
// The following object masquerades as an array.
var fakeArray = {"length": 1, 0: "Addy", 1: "Subtracty"};
// Therefore, convert it to a real array
var realArray = $.makeArray( fakeArray )
// Now it can be used reliably with $.map()
$.map( realArray, function(val, i) {
// do something
});
</pre>
<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>
<p>The function can return:</p>
<ul>
<li>the translated value, which will be mapped to the resulting array</li>
<li>
<code>null</code>, to remove the item</li>
<li>an array of values, which will be flattened into the full array</li>
</ul>
</div>
<h3>Examples:</h3>
<div class="entry-examples" id="entry-examples">
<div id="example-0">
<h4>Example: <span class="desc">A couple examples of using .map()</span>
</h4>
<pre class="prettyprint"><code class="example demo-code"><!DOCTYPE html>
<html>
<head>
<style>
div { color:blue; }
p { color:green; margin:0; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
<div></div>
<p></p>
<span></span>
<script>
var arr = [ "a", "b", "c", "d", "e" ];
$("div").text(arr.join(", "));
arr = jQuery.map(arr, function(n, i){
return (n.toUpperCase() + i);
});
$("p").text(arr.join(", "));
arr = jQuery.map(arr, function (a) {
return a + a;
});
$("span").text(arr.join(", "));
</script>
</body>
</html></code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div id="example-1">
<h4>Example: <span class="desc">Map the original array to a new one and add 4 to each value.</span>
</h4>
<pre class="prettyprint"><code class="example">$.map( [0,1,2], function(n){
return n + 4;
});</code></pre>
<h4>Result:</h4>
<pre><code class="results">[4, 5, 6] </code></pre>
</div>
<div id="example-2">
<h4>Example: <span class="desc">Maps the original array to a new one and adds 1 to each value if it is bigger then zero, otherwise it's removed.</span>
</h4>
<pre class="prettyprint"><code class="example">$.map( [0,1,2], function(n){
return n > 0 ? n + 1 : null;
});</code></pre>
<h4>Result:</h4>
<pre><code class="results">[2, 3] </code></pre>
</div>
<div id="example-3">
<h4>Example: <span class="desc">Map the original array to a new one; each element is added with its original value and the value plus one.</span>
</h4>
<pre class="prettyprint"><code class="example">$.map( [0,1,2], function(n){
return [ n, n + 1 ];
});</code></pre>
<h4>Result:</h4>
<pre><code class="results">[0, 1, 1, 2, 2, 3] </code></pre>
</div>
<div id="example-4">
<h4>Example: <span class="desc">Map the original object to a new array and double each value.</span>
</h4>
<pre class="prettyprint"><code class="example">
var dimensions = { width: 10, height: 15, length: 20 };
dimensions = $.map( dimensions, function( value, index ) {
return value * 2;
}); </code></pre>
<h4>Result:</h4>
<pre><code class="results">[20, 30, 40] </code></pre>
</div>
<div id="example-5">
<h4>Example: <span class="desc">Map an object's keys to an array.</span>
</h4>
<pre class="prettyprint"><code class="example">
var dimensions = { width: 10, height: 15, length: 20 },
keys = $.map( dimensions, function( value, index ) {
return index;
}); </code></pre>
<h4>Result:</h4>
<pre><code class="results">["width", "height", "length"] </code></pre>
</div>
<div id="example-6">
<h4>Example: <span class="desc">Maps the original array to a new one; each element is squared.</span>
</h4>
<pre class="prettyprint"><code class="example">
$.map( [0,1,2,3], function (a) {
return a * a;
});</code></pre>
<h4>Result:</h4>
<pre><code class="results">[0, 1, 4, 9] </code></pre>
</div>
<div id="example-7">
<h4>Example: <span class="desc">Remove items by returning null from the function. This removes any numbers less than 50, and the rest are decreased by 45.</span>
</h4>
<pre class="prettyprint"><code class="example">
$.map( [0, 1, 52, 97], function (a) {
return (a > 50 ? a - 45 : null);
});</code></pre>
<h4>Result:</h4>
<pre><code class="results">[7, 52] </code></pre>
</div>
<div id="example-8">
<h4>Example: <span class="desc">Augmenting the resulting array by returning an array inside the function.</span>
</h4>
<pre class="prettyprint"><code class="example">var array = [0, 1, 52, 97];
array = $.map(array, function(a, index) {
return [a - 45, index];
}); </code></pre>
<h4>Result:</h4>
<pre><code class="results">[-45, 0, -44, 1, 7, 2, 52, 3] </code></pre>
</div>
</div>
</div>
</div>
</div>
</body></html>
|