mirror of
https://github.com/jquery/jquery.git
synced 2026-01-27 18:08:10 -05:00
jQuery.map to conform with style guidelines - improved size/DRY code
This commit is contained in:
32
src/core.js
32
src/core.js
@@ -706,29 +706,31 @@ jQuery.extend({
|
||||
|
||||
// arg is for internal usage only
|
||||
map: function( elems, callback, arg ) {
|
||||
var ret = [],
|
||||
value,
|
||||
var ret = [], value, i = 0,
|
||||
length = elems.length,
|
||||
// same object detection used in jQuery.each, not full-proof but very speedy.
|
||||
isObj = length === undefined;
|
||||
|
||||
if ( isObj ) {
|
||||
for ( key in elems ) {
|
||||
value = callback( elems[ key ], key, arg );
|
||||
|
||||
|
||||
// the work for the loops - run elems[x] through callback
|
||||
inLoop = function( key ) {
|
||||
value = callback( elems[ key ], key, arg );
|
||||
|
||||
if ( value != null ) {
|
||||
ret[ ret.length ] = value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Go through the array, translating each of the items to their
|
||||
// new value (or values).
|
||||
for ( var i = 0; 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 ) {
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user