I am using jQuery's $.each
function to append (then delete
) each item as I iterate through an array. I then use an if
statement to stop iterating after deleting 8 objects. Here is a very simple example:
$.each(media, function(index, item) {
if (index < 8) {
$list.append(item);
delete media[index];
} else {
return false
}
});
The problem is if I run the code a second time, it runs eight times...but appends nothing. Here is a screenshot of from Chrome's console after the second run:
It appears as if this quirk has something to do with the fact that my indices don't re-sort/set after each time $.each
is used. Am I missing something or is anyone aware of an elegant workaround for this?
Use the shift function?
or
returns the same thing