This question already has an answer here:
I'm having a hard time understanding this code. Could someone try to explain why an array can have elements and 0 length?
var myArray = [];
myArray["hello"] = 4;
myArray["world"] = 65;
$('#btn').on('click',function() {
console.log(myArray.length); // Prints 0
console.log(myArray); // Prints [hello: 4, world: 65]
console.log(myArray.length); // Prints 0
}
Array
in this example as a dictionary. If you want to loop through theArray
, you could put theString
andNumber
into anObject
and insert that into theArray
. – Evin Ugur Jul 25 '14 at 16:14