Javascript:多维对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6643375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: multi-dimensional object
提问by user8363
I want to create an object, starting from something like:
我想创建一个对象,从以下内容开始:
var map = {};
Then, I want to add items with this function:
然后,我想用这个函数添加项目:
add = function(integerA, objectB) {
map[objectB.type][integerA] = objectB;
}
So, this is a random example of the object structure I want to achieve:
所以,这是我想要实现的对象结构的随机示例:
map = {
'SomeType' : { 0 : 'obj', 2 : 'obj', 3 : 'obj' },
'OtherType' : { 0 : 'obj', 5 : 'obj' },
};
Now, my problem. I can't do map[objectB.type][integerA] = objectB;
because map[objectB.type]
is not defined. I could solve this by checking if map[objectB.type]
exists through an if-statement and create map[objectB.type] = {};
when necessary.
现在,我的问题。我不能做,map[objectB.type][integerA] = objectB;
因为map[objectB.type]
没有定义。我可以map[objectB.type]
通过 if 语句检查是否存在并map[objectB.type] = {};
在必要时创建来解决这个问题。
Otherwise I could pre-load all object types. However I would prefer not to have to do this.
否则我可以预加载所有对象类型。但是,我宁愿不必这样做。
My question: is there a way I can create the object 'on the fly' without having to check if the type already exists every time I want to call the add
function or to pre-load all the types?
我的问题:有没有一种方法可以“即时”创建对象,而不必在每次我想调用add
函数或预加载所有类型时检查该类型是否已经存在?
It is important that my add function is so fast as possible and that the map object is correct, because I need to read and write a lot in a small amount of time (it's an animation / game application).
重要的是我的 add 函数尽可能快,地图对象正确,因为我需要在短时间内大量读写(它是一个动画/游戏应用程序)。
回答by bjornd
No, there is no any other way to create objects on the fly. Only check for existence every time:
不,没有任何其他方法可以即时创建对象。每次只检查是否存在:
add = function(integerA, objectB) {
if (!map[objectB.type]) {
map[objectB.type] = {};
}
map[objectB.type][integerA] = objectB;
}
If you want to improve performance you might consider some caching technics.
如果你想提高性能,你可以考虑一些缓存技术。
回答by Felix Kling
You can use the boolean OR shortcut (which avoids at least an explicit if
). It might not be that readable though:
您可以使用布尔 OR 快捷方式(至少避免了显式if
)。虽然它可能不那么可读:
var data = map[objectB.type] || (map[objectB.type] = {});
data[integerA] = objectB;
This works because an assignment actually returns the value that was assigned and an OR expression returns the first value that evaluates to true
.
这是有效的,因为赋值实际上返回分配的值,而 OR 表达式返回计算结果为 的第一个值true
。
I don't think using an if
has any impact on the performance though (actually, the way in my answer might be even "slower").
我不认为使用 anif
对性能有任何影响(实际上,我的答案中的方式可能甚至“更慢”)。
回答by casablanca
If you use the map only for lookups and you don't need to iterate over the dimensions, you could merge your dimensions into a single key. For example:
如果您仅将地图用于查找并且不需要迭代维度,则可以将维度合并为一个键。例如:
add = function(integerA, objectB) {
var key = objectB.type + '-' + integerA;
map[key] = objectB;
}