[英]How do I store complex objects in javascript?
I need to be able to store objects in javascript, and access them very quickly. 我需要能够在javascript中存储对象,并非常快速地访问它们。 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? 然后,我可以简单地说vehicles[0]
来获取福特福克斯的条目, vehicles[1]
来获取铃木雨燕的条目,等等。但是,我怎么知道福特福克斯是哪个条目?
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? 如何在javascript中做到这一点? Or, is there a better way? 或者,还有更好的方法?
Thanks. 谢谢。
Javascript objects can be used as hash tables: Javascript对象可以用作哈希表:
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];
As Rabbott said, JSON objects can be used like hashes. 正如Rabbott所说,JSON对象可以像哈希一样使用。
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. 这实际上与SLaks建议的表示几乎相同,但是“名称”将简单地存储为键,并且不会在值中重复。
As a note, I believe it is more proper to represent the keys as strings as I have done above ( "isDamaged"
instead of isDamaged
). 需要注意的是,我相信像我上面所做的那样将键表示为字符串更合适( "isDamaged"
而不是isDamaged
)。
您所显示的是一个javascript对象,对象只是散列。JSON(JavaScript对象表示法),请访问http://www.json.org/example.html
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.