aboutsummaryrefslogtreecommitdiffstats
path: root/src/core.js
diff options
context:
space:
mode:
authorDan Heberden <danheberden@gmail.com>2011-03-21 12:12:31 -0700
committerDan Heberden <danheberden@gmail.com>2011-03-21 12:12:31 -0700
commitd832f4f71ec51e67d4cae2557221ef582818f607 (patch)
treefd22a7d34543fc7334e7e288005c9ab224dd401e /src/core.js
parente38f074d14fd65b3f8b0e1bd7956cd75b3dafe2b (diff)
downloadjquery-d832f4f71ec51e67d4cae2557221ef582818f607.tar.gz
jquery-d832f4f71ec51e67d4cae2557221ef582818f607.zip
jQuery.map to iterate over objects with a .length property
Diffstat (limited to 'src/core.js')
-rw-r--r--src/core.js40
1 files changed, 20 insertions, 20 deletions
diff --git a/src/core.js b/src/core.js
index a0dd7b5b3..951f1b559 100644
--- a/src/core.js
+++ b/src/core.js
@@ -704,33 +704,33 @@ jQuery.extend({
return ret;
},
- // arg is for internal usage only
+ // arg is for internal usage only
map: function( elems, callback, arg ) {
- var ret = [], value, i = 0,
- length = elems.length,
- // same object detection used in jQuery.each, not full-proof but very speedy.
- isObj = length === undefined;
-
- // the work for the loops - run elems[x] through callback
- inLoop = function( key ) {
- value = callback( elems[ key ], key, arg );
-
+ var value, ret = [],
+ i = 0,
+ length = elems.length,
+ // process .length if it's just an object member
+ isArray = length !== undefined && ( elems[ length - 1 ] || jQuery.isArray( elems ) );
+
+ // Go through the array, translating each of the items to their
+ // new value (or values).
+ if ( isArray ) {
+ for ( ; i < length; i++ ) {
+ value = callback( elems[ i ], i, arg );
+
if ( value != null ) {
ret[ ret.length ] = value;
}
}
- // Go thorugh every key on the object
- if ( isObj ) {
+ // Go thorugh every key on the object,
+ } else {
for ( key in elems ) {
- inLoop( key );
- }
-
- // Go through the array, translating each of the items to their
- // new value (or values).
- } else {
- for ( ; i < length; i++ ) {
- inLoop( i );
+ value = callback( elems[ key ], key, arg );
+
+ if ( value != null ) {
+ ret[ ret.length ] = value;
+ }
}
}