#4176 closed bug (fixed)
jQuery.each() doesn't deal with arrays properly
Reported by: | wmleler | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | 1.3.2 |
Component: | core | Version: | 1.3.1 |
Keywords: | each array | Cc: | |
Blocked by: | Blocking: |
Description
If you create an empty array and insert values into it, then use the jQuery.each() utility, it has an extra value with index 0 that was not inserted. Here is code to show the bug:
<code>
$(function () {
var p1 = []; var p2 = {};
$.each(p1, function(i, v) {
$('#output').append('+i+? '+v+'<br />');
}); $('#output').append('<br />'); $.each(p2, function(i, v) {
$('#output').append('+i+? '+v+'<br />');
});
});
Output is:
Note the extra value in the array with index 0. That should not be there. Tested on Safari and Firefox on Macintosh.
Attachments (1)
Change History (5)
Changed 14 years ago by
comment:1 Changed 14 years ago by
Resolution: | → worksforme |
---|---|
Status: | new → closed |
jQuery.each
always processes an Array from 0 to length-1. The length property is managed by Javascript so it will equal the value of the highest numeric index plus 1. If you have a truly sparse array, you can always handle it with an object, as your example shows.
comment:2 Changed 14 years ago by
Resolution: | worksforme |
---|---|
Status: | closed → reopened |
Would be very nice if this were in the documentation. Something on the order of "each() iterates though arrays from 0 to length-1".
comment:3 Changed 14 years ago by
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
small program to reproduce the bug