javascript 使用自定义键创建对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22378950/
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
Create an array of Objects with custom keys
提问by abpetkov
Currently, I have an array of objects, that looks like this:
目前,我有一个对象数组,如下所示:
var arr = [{
id: UNIQUE_ID,
title: 'TITLE'
}, {
id: UNIQUE_ID,
title: 'TITLE'
}];
What is bothering me here, is that in particular cases I have to loop through the array and display data for matching ID. I want to just fetch everything for that particular object, thas has the ID I want, and that's it. It would be a lot easier for me if the array looked more like this:
在这里困扰我的是,在特定情况下,我必须遍历数组并显示匹配 ID 的数据。我只想获取那个特定对象的所有东西,它有我想要的 ID,就是这样。如果数组看起来更像这样,对我来说会容易得多:
var arr = [{
id: {
title: 'TITLE'
},
id: {
title: 'TITLE'
}
}]
The ID comes from a result of a method that generates a random number, so I'll need to put a variable or a method call for this id.
ID 来自生成随机数的方法的结果,因此我需要为此 ID 放置一个变量或方法调用。
I'm not sure this is possible, since I found no particular example on this, but I'd like to hear another solutions as well.
我不确定这是否可行,因为我没有找到关于此的特定示例,但我也想听听其他解决方案。
回答by T.J. Crowder
You can do that, by removing the array entirely, just using an object:
您可以通过完全删除数组来做到这一点,只需使用一个对象:
var items = {};
items["some id"] = {title: 'first title'};
items["another id"] = {title: 'second title'};
Or if you have the key in a variable:
或者,如果您在变量中有键:
var key = "a third id";
items[key] = {title: 'third title'};
Later, if you wanted to look up one of those entries based on a key:
稍后,如果您想根据键查找这些条目之一:
var key = "another id";
You'd do it like this:
你会这样做:
console.log(items[key].title); // "second title" (if key is "another id")
If you need to know what all of the keys in the object are, you can use Object.keys
:
如果您需要知道对象中的所有键是什么,您可以使用Object.keys
:
var arrayOfKeys = Object.keys(obj);
(Note that Object.keys
is an ES5 feature; on older browsers, you need to add a shim for it, which is trivial.)
(请注意,这Object.keys
是 ES5 功能;在较旧的浏览器上,您需要为它添加一个 shim,这是微不足道的。)
Note the way I populated the object in the first code block above. If you can hardcode the keys, you can do that with an object initializer:
请注意我在上面第一个代码块中填充对象的方式。如果您可以对键进行硬编码,则可以使用对象初始值设定项来实现:
var items = {
"some id": {title: 'first title'},
"another id": {title: 'second title'}
};
...but you can't do it that way if you want to use a variable for the key name, because you can't put the variable on the left-hand side of the :
(it'll look like a literal key name). That is, if you have
...但是如果你想使用一个变量作为键名,你不能这样做,因为你不能把变量放在:
(它看起来像一个字面键名)的左侧)。也就是说,如果你有
var key = "a third id";
then this won't work:
那么这将不起作用:
var items {
key: {title: "third title"} // <==== Doesn't do what we want
};
The key would be "key"
, not "a third id"
(just like the title
key we've been using). That's why in the first block above, I created the object and then set the properties separately afterward.
关键是"key"
, 不是"a third id"
(就像title
我们一直在使用的关键)。这就是为什么在上面的第一个块中,我创建了对象,然后单独设置了属性。
回答by Samuel
What you're asking for far is call indexing. You can easily achieve it by structuring your data as such:
你所要求的是调用索引。您可以通过将数据结构化来轻松实现它:
var arr = {
1: {
title: 'TITLE'
},
2: {
title: 'TITLE'
}
};
You don't even have to traverse the now indexed array, since each entry is accessible through its index:
您甚至不必遍历现在索引的数组,因为每个条目都可以通过其索引访问:
var needle = array[id];
回答by Borys Generalov
With the help of UnderscoreJs:
在 UnderscoreJs 的帮助下:
var arr= [{
id: '1',
title: 40
}, {
id: '2',
title: 40
}, {
id: '1',
title: 60
}];
var res = _.groupBy(arr, function(x){ return x.id; });
res["1"];
Result:
结果:
[{
id: '1',
title: 40
}, {
id: '1',
title: 60
}]