Bug Tracker

Ticket #2616: map.diff

File map.diff, 1.3 kB (added by flesler, 10 months ago)

Proposal

  • core.js

     
    11891189        return ret; 
    11901190    }, 
    11911191 
    1192     map: function( elems, callback ) { 
    1193         var ret = []; 
     1192    map: function( source, callback, target ) { 
     1193        if( !target ) 
     1194            target = 'length' in source ? [ ] : { }; 
     1195         
     1196        if( 'length' in target ){ //is it an array(-like) ? else a hash 
     1197            var arr = true; 
     1198            if( !target.concat )//fix array-like objects, or they will fail the final concat 
     1199                target = jQuery.makeArray(target); 
     1200        } 
    11941201 
    1195         // Go through the array, translating each of the items to their 
     1202        // Go through the items, translating each of them to their 
    11961203        // new value (or values). 
    1197         for ( var i = 0, length = elems.length; i < length; i++ ) { 
    1198             var value = callback( elems[ i ], i ); 
     1204        jQuery.each( source , function( key, value ){ 
     1205            value = callback( value, key ); 
     1206             
     1207            if ( value != null ) 
     1208                target[ arr ? target.length : key ] = value; 
     1209        }); 
    11991210 
    1200             if ( value !== null && value != undefined ) { 
    1201                 if ( value.constructor != Array ) 
    1202                     value = [ value ]; 
    1203  
    1204                 ret = ret.concat( value ); 
    1205             } 
    1206         } 
    1207  
    1208         return ret; 
     1211        //Array.concat can receive arrays and non-arrays too 
     1212        return arr ? target.concat.apply( [], target ) : target; 
    12091213    } 
    12101214}); 
    12111215