This is weird. Check this out:
for( var i = 0; i <= videos.length; i ++ ){
alert(videos[i].id); // this works and alerts the correct number
var foo = videos[i].id; // firebug says "videos[i] is undefined"
}
There are 3 videos. In FF this alerts all 3 video id’s then fails saying that videos[i] is undefined. No Idea, at all.
Get rid of the
=in yourforloop condition.With
<=, you are iterating to the index, one larger than the actual index value of theArray, so you are iterating over an invalid index which returnsundefined.For example…
If you have
array('A','B','C'), the length is3. Now, if you iterate to3 <= i, and include0, as arrays begin with in Javascript, you will actually loop 4 times, not three.The index value of
Ais0, not1, so you need to stop BEFOREiis equal to the length, not continue untiliis equal to the length, since the0index is essentially added to the total length for the looping, meaning 3+1. 4 loops over this array would be one too many, hence the<and not<=. You want to STOP before 4, not stop AFTER 4 but before 5.Also, it is generally good practice to cache the length of the
Array, because some browsers do not optimise it.