Javascript 数组键查找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18677930/
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 Array Key Lookup
提问by Andy Phillips
I'm sorry if this has been asked before, it's something that's difficult to search for...
很抱歉,如果之前有人问过这个问题,这很难搜索......
I want to use a javascript Array to hold objects, with the key as the ID
我想使用一个 javascript 数组来保存对象,以键为 ID
for example, let's say I had a bunch of people who had different IDs
例如,假设我有一群拥有不同 ID 的人
var people = new Array();
var person = {property: value}; // this is person ID 4
var people[4] = person;
I want to be able to then reference that user by saying, people[ID].propery
我希望能够通过说 people[ID].propery 来引用该用户
The problem is that the output of this array now would be;
问题是这个数组的输出现在是;
null,null,null,null,object
Because it's expecting the keys to be 0,1,2,3,4
因为它期望键是 0,1,2,3,4
Am I being stupid or something? :-) We can do it for strings right, so why not non-sequential numbers?
是我傻还是什么?:-) 我们可以对字符串进行正确的处理,那么为什么非序列数不能呢?
What I'm trying to avoid is having to loop over every single object in the array every time I want to access a particular person inside it, therefore I figured that using the ID number as the key would work
我试图避免的是每次我想访问数组中的特定人时都必须循环遍历数组中的每个对象,因此我认为使用 ID 号作为键是可行的
Thanks guys! :-)
多谢你们!:-)
回答by Adam
Use a dictionary object
使用字典对象
var people = {};
people[4] = 'me';
回答by elclanrs
I'd suggest you use collections. A collection is an array of objects. You can then pass properties on each person. You can filter your collection by any property. So close to what you're doing, but instead of relaying on the index, pass the id
for each person.
我建议你使用集合。集合是一组对象。然后,您可以为每个人传递属性。您可以按任何属性过滤您的收藏。非常接近您正在做的事情,但不要传递索引,而是id
为每个人传递。
var people = []; // a collection
var person = {
id: 4,
name: 'John'
};
people.push(person);
// Filtering:
// By id
var john = people.filter(function(person) {
return person.id == 4;
});
// By name
var john = people.filter(function(person) {
return person.name == 'John';
});
You can abstract those loops above to re-use them. Also make sure your id's are unique. If data is coming from the DB it should be OK, otherwise I'd keep track of them somewhere.
您可以抽象上面的那些循环以重新使用它们。还要确保您的 ID 是唯一的。如果数据来自数据库,那应该没问题,否则我会在某处跟踪它们。
The advantage of collections, as opposed to a plain object with keys is that you can sort and filter, while an object, where the order of properties is not guaranteed, you can't do it as simple.
与带有键的普通对象相比,集合的优点是您可以排序和过滤,而不能保证属性顺序的对象则不能这么简单。
Note that filter
only works on "modern browsers", so IE9+, but there are polyfillsfor older browsers.
请注意,filter
仅适用于“现代浏览器”,即 IE9+,但有适用于旧浏览器的polyfill。
回答by grape_mao
when we use a string as key, like this:
当我们使用字符串作为键时,像这样:
var people = {}; //this is an object
people[name] = 'toto';
we are adding new property to this object, because each object in javascript can be seen as a hashtable.
我们正在向这个对象添加新属性,因为 JavaScript 中的每个对象都可以看作是一个哈希表。
If you use an array, it's still an object, you can add properties to it using string as key. But if you do something like people[4] = 'toto';
, you are adding a string to the array, the length of the array will then become 5. Of course the number 4 will still be a property of this array object.
如果您使用数组,它仍然是一个对象,您可以使用字符串作为键向其添加属性。但是如果你做类似的事情people[4] = 'toto';
,你在数组中添加一个字符串,那么数组的长度就会变成 5。当然,数字 4 仍然是这个数组对象的一个属性。