mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Merge branch 'map-object.1.6' of https://github.com/danheberden/jquery into danheberden-map-object.1.6
This commit is contained in:
commit
247363b9a7
21
src/core.js
21
src/core.js
@ -710,11 +710,15 @@ jQuery.extend({
|
||||
|
||||
// arg is for internal usage only
|
||||
map: function( elems, callback, arg ) {
|
||||
var ret = [], value;
|
||||
var value, key, ret = [],
|
||||
i = 0,
|
||||
length = elems.length,
|
||||
// jquery objects are treated as arrays
|
||||
isArray = elems instanceof jQuery || length !== undefined && typeof length === "number" && ( ( length > 0 && elems[ 0 ] && elems[ length -1 ] ) || jQuery.isArray( elems ) ) ;
|
||||
|
||||
// Go through the array, translating each of the items to their
|
||||
// new value (or values).
|
||||
for ( var i = 0, length = elems.length; i < length; i++ ) {
|
||||
if ( isArray ) {
|
||||
for ( ; i < length; i++ ) {
|
||||
value = callback( elems[ i ], i, arg );
|
||||
|
||||
if ( value != null ) {
|
||||
@ -722,6 +726,17 @@ jQuery.extend({
|
||||
}
|
||||
}
|
||||
|
||||
// Go thorugh every key on the object,
|
||||
} else {
|
||||
for ( key in elems ) {
|
||||
value = callback( elems[ key ], key, arg );
|
||||
|
||||
if ( value != null ) {
|
||||
ret[ ret.length ] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Flatten any nested arrays
|
||||
return ret.concat.apply( [], ret );
|
||||
},
|
||||
|
@ -642,7 +642,7 @@ test("first()/last()", function() {
|
||||
});
|
||||
|
||||
test("map()", function() {
|
||||
expect(2);//expect(6);
|
||||
expect(7);
|
||||
|
||||
same(
|
||||
jQuery("#ap").map(function(){
|
||||
@ -660,32 +660,32 @@ test("map()", function() {
|
||||
"Single Map"
|
||||
);
|
||||
|
||||
return;//these haven't been accepted yet
|
||||
|
||||
//for #2616
|
||||
var keys = jQuery.map( {a:1,b:2}, function( v, k ){
|
||||
return k;
|
||||
}, [ ] );
|
||||
|
||||
});
|
||||
equals( keys.join(""), "ab", "Map the keys from a hash to an array" );
|
||||
|
||||
var values = jQuery.map( {a:1,b:2}, function( v, k ){
|
||||
return v;
|
||||
}, [ ] );
|
||||
|
||||
});
|
||||
equals( values.join(""), "12", "Map the values from a hash to an array" );
|
||||
|
||||
// object with length prop
|
||||
var values = jQuery.map( {a:1,b:2, length:3}, function( v, k ){
|
||||
return v;
|
||||
});
|
||||
equals( values.join(""), "123", "Map the values from a hash with a length property to an array" );
|
||||
|
||||
var scripts = document.getElementsByTagName("script");
|
||||
var mapped = jQuery.map( scripts, function( v, k ){
|
||||
return v;
|
||||
}, {length:0} );
|
||||
|
||||
});
|
||||
equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );
|
||||
|
||||
var flat = jQuery.map( Array(4), function( v, k ){
|
||||
return k % 2 ? k : [k,k,k];//try mixing array and regular returns
|
||||
});
|
||||
|
||||
equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user