简体   繁体   English

JavaScript阵列键查询

[英]Javascript Array Key Lookup

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! 多谢你们! :-) :-)

Use a dictionary object 使用字典对象

var people = {};
people[4] = 'me';

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. 另外,请确保您的ID是唯一的。 If data is coming from the DB it should be OK, otherwise I'd keep track of them somewhere. 如果数据来自数据库,那应该没问题,否则我会在某个地方跟踪它们。

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 polyfills for older browsers. 请注意, filter仅适用于“现代浏览器”,即IE9 +,但是对于较旧的浏览器有polyfills

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'; 但是,如果您执行类似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. ,您将一个字符串添加到数组中,那么数组的长度将变为5。当然,数字4仍然是此数组对象的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM