Colen
Colen

Reputation: 13898

How do I store complex objects in javascript?

I need to be able to store objects in javascript, and access them very quickly. For example, I have a list of vehicles, defined like so:

{ "name": "Jim's Ford Focus", "color": "white", isDamaged: true, wheels: 4 }
{ "name": "Bob's Suzuki Swift", "color": "green", isDamaged: false, wheels: 4 }
{ "name": "Alex's Harley Davidson", "color": "black", isDamaged: false, wheels: 2 }

There will potentially be hundreds of these vehicle entries, which might be accessed thousands of times. I need to be able to access them as fast as possible, ideally in some useful way.

For example, I could store the objects in an array. Then I could simply say vehicles[0] to get the Ford Focus entry, vehicles[1] to get the Suzuki Swift entry, etc. However, how do I know which entry is the Ford Focus?

I want to simply ask "find me Jim's Ford Focus" and have the object returned to me, as fast as possible. For example, in another language, I might use a hash table, indexed by name. How can I do this in javascript? Or, is there a better way?

Thanks.

Upvotes: 2

Views: 13519

Answers (4)

aekeus
aekeus

Reputation: 296

Another interesting way to approach this is to build a generic indexing function. This function will accept an array of objects and a key field name. It will store each of the records in an array stored in an object keyed on the passed in key field name.

Code follows:

var cars = [
  { "name": "Jim's Ford Focus",       "color": "white", "isDamaged": true,  "wheels": 4 },
  { "name": "Bob's Suzuki Swift",     "color": "green", "isDamaged": false, "wheels": 4 },
  { "name": "Alex's Harley Davidson", "color": "black", "isDamaged": false, "wheels": 2 }
];

function buildIndex(items, key) {
  var index = {},
      idx,
      item;
  for (idx in items) {
    item = items[idx];
    if(index[items[idx][key]] === undefined) {
      index[items[idx][key]] = [item];
    } else {
      index[items[idx][key]].push(item);
    }
  }
  return index;
}

var indexedByWheels = buildIndex(cars, "wheels");

/*

indexedByWheels will be structured as:

{
  "4": [
    { "name": "Jim's Ford Focus",       "color": "white", "isDamaged": true,  "wheels": 4 },
    { "name": "Bob's Suzuki Swift",     "color": "green", "isDamaged": false, "wheels": 4 }
  ],
  "2": [
    { "name": "Alex's Harley Davidson", "color": "black", "isDamaged": false, "wheels": 2 }
  ]
];

*/

for (var v in indexedByWheels) {
  print("Wheels " + v)  ;
  for (var i in indexedByWheels[v]) {
    print("  Car: " + indexedByWheels[v][i].name);
  }
}

Upvotes: 1

Trey Hunner
Trey Hunner

Reputation: 11804

As Rabbott said, JSON objects can be used like hashes.

Try something like this to store your data:

vehicles = {
  "Jim's Ford Focus": { "color": "white", "isDamaged": true, "wheels": 4 },
  "Bob's Suzuki Swift": { "color": "green", "isDamaged": false, "wheels": 4 },
  "Alex's Harley Davidson": { "color": "black", "isDamaged": false, "wheels": 2 }
};
document.write(vehicles["Jim's Ford Focus"]["color"]);

This is actually nearly the same representation as SLaks suggested but the "name" will simply be stored as a key and not duplicated within the value.

As a note, I believe it is more proper to represent the keys as strings as I have done above ("isDamaged" instead of isDamaged).

Upvotes: 3

Rabbott
Rabbott

Reputation: 4332

What you have shown IS a javascript object, objects are just hashs.. JSON (JavaScript Object Notation), check out http://www.json.org/example.html

Upvotes: 1

SLaks
SLaks

Reputation: 887275

Javascript objects can be used as hash tables:

var nameIndex = {};

for (var i = 0; i < cars.length; i++)
    nameIndex[cars[i].name] = cars[i];

//Later:
var someName = "Jim's Ford Focus";
var car = nameIndex[someName];

Upvotes: 4

Related Questions