I'm sorry if this has been asked before, it's something that's difficult to search for...
I want to use a javascript Array to hold objects, with the key as the ID
for example, let's say I had a bunch of people who had different IDs
var people = new Array();
var person = {property: value}; // this is person ID 4
var people[4] = person;
I want to be able to then reference that user by saying, people[ID].propery
The problem is that the output of this array now would be;
null,null,null,null,object
Because it's expecting the keys to be 0,1,2,3,4
Am I being stupid or something? :-) We can do it for strings right, so why not non-sequential numbers?
What I'm trying to avoid is having to loop over every single object in the array every time I want to access a particular person inside it, therefore I figured that using the ID number as the key would work
Thanks guys! :-)
output of arraymean? Yes, arrays in JS are sparse, and you might better consider using objects in this case, but I admit I still don't understand what's wrong in the example you've shown.null,null,null,null,object" - Arrays don't really have an "output". Your array would have one element at index 4, and if you tried to access the lower indexes, e.g.,people[2]you'd getundefined, notnull. Also you have a syntax error invar people[4] = person;, you need to removevar.