How do I store complex objects in javascript?
- by Colen
Hello,
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.