TypeScript 访问字典类型对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17367636/
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
TypeScript accessing dictionary type object
提问by ThunderDev
I need a sort of dictionary in my class (I'm not sure has this kind of object is called), I'm using this:
我的班级需要一种字典(我不确定是否调用了这种对象),我正在使用这个:
inputCollection: { [name: string]: HTMLInputElement };
inputCollection: { [name: string]: HTMLInputElement };
I can get and set values like so:
我可以像这样获取和设置值:
this.inputCollection['inputId'] = document.getElementById('inputId');
this.inputCollection['inputId'] = document.getElementById('inputId');
I would like to access my dictionary in the following manner, to get the first element
我想通过以下方式访问我的字典,以获取第一个元素
this.inputCollection[0]
this.inputCollection[0]
How could I achieve this easily ?
我怎么能轻易做到这一点?
回答by Fenton
If you want to obtain items using either a string or an index, you'll have to implement a Dictionary. There is no framework class library for TypeScript, so you don't get this for free - just the language features to make one.
如果要使用字符串或索引获取项目,则必须实现字典。没有用于 TypeScript 的框架类库,因此您不会免费获得它 - 只是制作一个的语言功能。
You can technically use both string and number indexers, but the first string item is not the same as the first number item. Example:
从技术上讲,您可以同时使用字符串索引器和数字索引器,但第一个字符串项与第一个数字项不同。例子:
class Item {
constructor(public name: string) {
}
}
var items = [];
items['a'] = new Item('One');
items['b'] = new Item('Two');
items[0] = new Item('Three');
alert(items['a'].name); // One
alert(items[0].name); // Three
Here is a really basic Dictionary class.
这是一个非常基本的 Dictionary 类。
class Item {
constructor(public name: string) {
}
}
class Dictionary<T> {
private items = [];
add(key: string, value: T) {
this.items.push(value);
this.items[key] = value;
}
getByIndex(index: number) {
return this.items[index];
}
getByKey(key: string) {
return this.items[key];
}
}
var dictionary = new Dictionary<Item>();
dictionary.add('a', new Item('One'));
dictionary.add('b', new Item('Two'));
alert(dictionary.getByIndex(0).name);
alert(dictionary.getByKey('a').name);
This also works if you change an item...
如果您更改项目,这也适用...
dictionary.add('a', new Item('One'));
dictionary.getByIndex(0).name = 'Changed';
alert(dictionary.getByIndex(0).name);
alert(dictionary.getByKey('a').name);
回答by andyks
Take a squizzy at basarat's TypeScript Collections lib
看看 basarat 的TypeScript Collections lib
If you are working on 0.9.0 you will need to make a few minor fixes but it is good.
如果你在 0.9.0 上工作,你需要做一些小的修复,但这很好。
回答by basarat
There is a difference between arrays an objects (what you are using). Object properties are not indexed. i.e. The first element you add is not necessarily in the first memory location. So you cannot do that. If you want such a functionality, use an array instead of dictionary object.
数组和对象(您正在使用的对象)之间存在差异。对象属性未编入索引。即您添加的第一个元素不一定在第一个内存位置。所以你不能那样做。如果您想要这样的功能,请使用数组而不是字典对象。