Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Array Key Lookup

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

like image 227
Andy Phillips Avatar asked Sep 07 '13 21:09

Andy Phillips


People also ask

How do you check if a key exists in an array JavaScript?

Using the indexOf() Method JavaScript's indexOf() method will return the index of the first instance of an element in the array. If the element does not exist then, -1 is returned.

How do you find array keys?

The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned. Specified array.

How do you get the key of an array of objects?

For getting all of the keys of an Object you can use Object. keys() . Object. keys() takes an object as an argument and returns an array of all the keys.

How do you find an element in an array?

JavaScript Array find()The find() method returns the value of the first element that passes a test. The find() method executes a function for each array element. The find() method returns undefined if no elements are found. The find() method does not execute the function for empty elements.


2 Answers

Use a dictionary object

var people = {};
people[4] = 'me';
like image 140
Adam Avatar answered Oct 10 '22 19:10

Adam


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.

like image 25
elclanrs Avatar answered Oct 10 '22 17:10

elclanrs