I am trying to set up an array in jQuery and I then need to do a for loop on it. But it seems that I cant use an associative array for some reason?
var items = new Array();
items['foo'] = 123456;
items['bar'] = 789012;
items['baz'] = 345678;
items['bat'] = 901234;
alert(items.length);
This is just a test, but it return 0?
You can’t make
associative arrayin JavaScript like what you want, instead you can use Object.For example:
And to calculate the length you can do:
Use:
getObjectSize(items); // output: 4For more see here.
Another one is:
But not supported by all browsers.