TypeScript 哈希图/字典接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42211175/
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 hashmap/dictionary interface
提问by mysticalstick
I'm new to using TypeScript and I'm trying to implement a hashmap/dictionary interface. So far I have
我是使用 TypeScript 的新手,我正在尝试实现一个 hashmap/dictionary 接口。到目前为止我有
export interface IHash {
[details: string] : string;
}
I'm having some trouble understanding what exactly this syntax means. If I were to do var x : IHash = {};
how would I go about adding/accessing the data?
我在理解此语法的确切含义时遇到了一些麻烦。如果我要这样做var x : IHash = {};
,我将如何添加/访问数据?
回答by Meirion Hughes
Just as a normal js object:
就像一个普通的 js 对象:
let myhash: IHash = {};
myhash["somestring"] = "value"; //set
let value = myhash["somestring"]; //get
There are two things you're doing with [indexer: string] : string
你正在做两件事 [indexer: string] : string
- tell TypeScript that the object can have any string-based key
- that for all key entries the value MUST be a string type.
- 告诉 TypeScript 该对象可以有任何基于字符串的键
- 对于所有键条目,值必须是字符串类型。
You can make a general dictionary with explicitly typed fields by using [key: string]: any;
您可以使用显式类型的字段制作通用字典 [key: string]: any;
e.g. age
must be number
, while name
must be a string - both are required. Any implicit field can be any type of value.
例如age
必须是number
,而name
必须是字符串——两者都是必需的。任何隐式字段都可以是任何类型的值。
As an alternative, there is a Map
class:
作为替代,有一个Map
类:
let map = new Map<object, string>();
let key = new Object();
map.set(key, "value");
map.get(key); // return "value"
This allows you have any Object instance(not just number/string) as the key.
这允许您将任何 Object实例(不仅仅是数字/字符串)作为键。
Although its relatively new so you may have to polyfill it if you target old systems.
尽管它相对较新,因此如果您针对旧系统,则可能必须对其进行 polyfill。
回答by C Smith
var x : IHash = {};
x['key1'] = 'value1';
x['key2'] = 'value2';
console.log(x['key1']);
// outputs value1
console.log(x['key2']);
// outputs value2
If you would like to then iterate through your dictionary, you can use.
如果你想然后遍历你的字典,你可以使用。
Object.keys(x).forEach((key) => {console.log(x[key])});
Object.keys returns all the properties of an object, so it works nicely for returning all the values from dictionary styled objects.
Object.keys 返回对象的所有属性,因此它可以很好地返回字典样式对象的所有值。
You also mentioned a hashmap in your question, the above definition is for a dictionary style interface. Therefore the keys will be unique, but the values will not.
您在问题中还提到了 hashmap,上面的定义是针对字典样式的界面。因此键是唯一的,但值不是。
You could use it like a hashset by just assigning the same value to the key and its value.
您可以像散列集一样使用它,只需将相同的值分配给键及其值即可。
if you wanted the keys to be unique and with potentially different values, then you just have to check if the key exists on the object before adding to it.
如果您希望键是唯一的并且具有可能不同的值,那么您只需在添加对象之前检查键是否存在于对象上。
var valueToAdd = 'one';
if(!x[valueToAdd])
x[valueToAdd] = valueToAdd;
or you could build your own class to act as a hashset of sorts.
或者您可以构建自己的类来充当各种哈希集。
Class HashSet{
private var keys: IHash = {};
private var values: string[] = [];
public Add(key: string){
if(!keys[key]){
values.push(key);
keys[key] = key;
}
}
public GetValues(){
// slicing the array will return it by value so users cannot accidentally
// start playing around with your array
return values.slice();
}
}