Side navigation
#4319 closed bug (invalid)
Opened March 10, 2009 10:19AM UTC
Closed March 14, 2009 01:52AM UTC
$.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)
Change History (2)
Changed March 10, 2009 10:33AM UTC by comment:1
Changed March 14, 2009 01:52AM UTC by comment:2
resolution: | → invalid |
---|---|
status: | new → closed |
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.
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 );
}
...