简体   繁体   English

如何通过名称而不是键号从数组中获取值

[英]How to get a value from an array by name instead of key-number

I've created an object and added an asset b345 with some properties in it: 我创建了一个对象,并添加了带有一些属性的资产b345

asset = [];
asset.push({'b345' : { 'prop1':'value 1', 'prop2': null}});

I want to push some more assets later, dynamicaly into the same asset-object. 我想稍后再动态地将更多资产推入相同的资产对象。 So that the asset object holds all assets generated in the code. 这样asset对象将保存代码中生成的所有资产。

Later on in my code I want to retrieve the array associated with one of the entries via the unique identifier b345 . 稍后在我的代码中,我想通过唯一标识符b345检索与条目之一关联的数组。 But that seems not to work. 但这似乎不起作用。

asset['b345'];

But that results in an undefined . 但这导致undefined But If I try to get the data via asset[0] It returns the right object. 但是,如果我尝试通过asset[0]获取数据,它将返回正确的对象。

How could I arrange this container object asset so that I can 我如何安排该容器对象asset以便我可以
1- easliy add new objects 1-轻松添加新对象
2- retrieve the object via the identifier? 2-通过标识符检索对象?

ps: I found this: https://npmjs.org/package/hashtable but it is only usefull for large storage; ps:我发现了这一点: https//npmjs.org/package/hashtable,但这仅适用于大型存储; and it says it can be done through objects only. 它说只能通过对象来完成。 But I can't find how it works :( 但是我找不到它是如何工作的:(

Instead of using an array you use an object 使用对象代替使用数组

asset = {};
asset['b345'] =  { 'prop1':'value 1', 'prop2': null};

then asset['b345'] will give you { 'prop1':'value 1', 'prop2': null} 那么asset['b345']会给您{ 'prop1':'value 1', 'prop2': null}

If you have no need to iterate through your list of objects in some consistent order, then you probably shouldn't make an array in the first place; 如果您不需要以某种一致的顺序遍历对象列表,则可能不应该首先创建数组。 just make an object: 只是做一个对象:

var asset = {};
asset['b345'] = { 'prop1':'value 1', 'prop2': null};

If you do need to have both array behavior and key-lookup, an efficient way to do it would be to make an array and an object: 如果确实需要同时具有数组行为和键查找,那么一种有效的方法是制作一个数组一个对象:

var assets = {
  list: []
, map: {}
, push: function(key, value) {
    map[key] = value;
    list.push({ key: value });
  }
};

Then: 然后:

assets.push('b345', { 'prop1': 'value 1', 'prop2': null });

Subsequently assets.list[0] will be { b345: { prop1: 'value 1', prop2: null }} and assets.map['b345'] will be {prop1: 'value 1', 'prop2': null} . 随后, assets.list[0]将为{ b345: { prop1: 'value 1', prop2: null }}assets.map['b345']将为{prop1: 'value 1', 'prop2': null}

You'd want to make it a little more sophisticated to deal with updates properly, but that's the basic pattern. 您希望使它能够更正确地处理更新,但这是基本模式。

There's many way to do this. 有很多方法可以做到这一点。 To simply fix this problem: 简单地解决这个问题:

var asset = new Object; // new object
asset['b345'] = {'prop1':'value 1', 'prop2': null}; // an object property = new object (wrapped in {})

Another way, is: 另一种方法是:

asset = {'b345' : { 'prop1':'value 1', 'prop2': null}};

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

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