I’m creating a playlist system where each song has a unique ID. When you add a song to the playlist array, its index is registered as its ID.
Normally when looping over a javascript array, you grab the length and count up through the index. Is there a way to loop through an array with unused indexes? Is that bad practice for any reason?
Use
for(var i in obj)to loop through an object. See the comments above to understand the meaning of thisfor-statement.You’re talking about Objects, not arrays, by the way:
An array can be simulated in this way, so that a
for(var i=0; i<array_like.length; i++)can be used. Array functions (such aspop) cannot be used on them, unless you define it: