Bug Tracker

Opened 11 years ago

Closed 11 years ago

#4319 closed bug (invalid)

$.each can't work with Associative array

Reported by: chuiwenchiu Owned by:
Priority: major Milestone: 1.3.2
Component: core Version: 1.3.1
Keywords: Cc:
Blocked by: Blocking:

Description

My Test Enviroment: IE7 jQuery Version: 1.3.1 & 1.3.2

Description: if i use "Associative array" in each(), the method not work is it bug ? Test Code: var a = [ 'a' ]; a[a.length] = 'b'; a[2] = 'c'; a[ '3' ] = 'd';

a[ 'four' ] = 'e';

fail: skip a[ 'four' ]

$.each(a, function(i, val) {

alert('Test#5 >> ' + i + ':' + val);

});

var b = []; b[ 'one' ] = 1; b[ 'two' ] = 2;

$.each(b, function(i, val) {

fail: no run here

alert('Test#6 >> ' + i + ':' + val);

});

Attachments (1)

test_jquery132.html (1.2 KB) - added by chuiwenchiu 11 years ago.

Download all attachments as: .zip

Change History (3)

Changed 11 years ago by chuiwenchiu

Attachment: test_jquery132.html added

comment:1 in reply to:  description Changed 11 years ago by chuiwenchiu

each() use 'length' property detect Array or Object,


if ( length === undefined ) {

for ( name in object )

if ( callback.call( object[ name ], name, object[ name ] ) === false )

break;

} else

for ( var value = object[0];

i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}

---

BUT ASSOCIATIVE ARRAY has 'length' property, but not use object[i] get eleemnt of array, need to use

for(var prop in b ){

alert( prop );

}

...

comment:2 Changed 11 years ago by dmethvin

Resolution: invalid
Status: newclosed

Per the documentation, if the object you pass in has a length property, the object will be treated as an array and iterated from 0 to length-1.

http://docs.jquery.com/Utilities/jQuery.each#objectcallback

Note: See TracTickets for help on using tickets.