6

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! :-)

2
  • 1
    What's output of array mean? 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. Commented Sep 7, 2013 at 21:13
  • 1
    "the output of this array now would be 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 get undefined, not null. Also you have a syntax error in var people[4] = person;, you need to remove var. Commented Sep 7, 2013 at 22:53

3 Answers 3

9

Use a dictionary object

var people = {};
people[4] = 'me';
Sign up to request clarification or add additional context in comments.

Comments

3

I'd suggest you use collections. A collection is an array of objects. You can then pass properties on each person. You can filter your collection by any property. So close to what you're doing, but instead of relaying on the index, pass the id for each person.

var people = [];  // a collection

var person = {
  id: 4,
  name: 'John'
};

people.push(person);

// Filtering:

// By id
var john = people.filter(function(person) {
  return person.id == 4;
});

// By name
var john = people.filter(function(person) {
  return person.name == 'John';
});

You can abstract those loops above to re-use them. Also make sure your id's are unique. If data is coming from the DB it should be OK, otherwise I'd keep track of them somewhere.

The advantage of collections, as opposed to a plain object with keys is that you can sort and filter, while an object, where the order of properties is not guaranteed, you can't do it as simple.

Note that filter only works on "modern browsers", so IE9+, but there are polyfills for older browsers.

Comments

1

when we use a string as key, like this:

var people = {}; //this is an object
people[name] = 'toto';

we are adding new property to this object, because each object in javascript can be seen as a hashtable.

If you use an array, it's still an object, you can add properties to it using string as key. But if you do something like people[4] = 'toto';, you are adding a string to the array, the length of the array will then become 5. Of course the number 4 will still be a property of this array object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.