键值对在 Typescript 中可用吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36467469/
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
Is key-value pair available in Typescript?
提问by CodeHunter
Is key,value pair available in typescript? If yes how to do that. Can anyone provide sample example links.
键值对在打字稿中可用吗?如果是,该怎么做。任何人都可以提供示例示例链接。
回答by basarat
Is key-value pair available in Typescript?
键值对在 Typescript 中可用吗?
Yes. Called an index signature:
是的。称为索引签名:
interface Foo {
[key: string]: Bar;
}
let foo:Foo = {};
Here keys are string
and values are Bar
.
这里的键是string
,值是Bar
。
More
更多的
You can use an es6 Map
for proper dictionaries, polyfilled by core-js
.
您可以将 es6Map
用于适当的字典,由core-js
.
回答by Jaime
The simplest way would be something like:
最简单的方法是这样的:
var indexedArray: {[key: string]: number}
Usage:
用法:
var indexedArray: {[key: string]: number} = {
foo: 2118,
bar: 2118
}
indexedArray['foo'] = 2118;
indexedArray.foo= 2118;
let foo = indexedArray['myKey'];
let bar = indexedArray.myKey;
回答by Hyman Miller
Is key-value pair available in Typescript?
键值对在 Typescript 中可用吗?
If you think of a C# KeyValuePair<string, string>: No, but you can easily define one yourself:
如果您想到 C# KeyValuePair<string, string>:不,但您可以轻松地自己定义一个:
interface KeyValuePair {
key: string;
value: string;
}
Usage:
用法:
let foo: KeyValuePair = { key: "k", value: "val" };
回答by peter70
Not for the questioner, but for all others, which are interested: See: How to define Typescript Map of key value pair. where key is a number and value is an array of objects
不是针对提问者,而是针对所有其他感兴趣的人:请参阅:如何定义键值对的 Typescript Map。其中键是一个数字,值是一个对象数组
The solution is therefore:
因此,解决方案是:
let yourVar: Map<YourKeyType, YourValueType>;
// now you can use it:
yourVar = new Map<YourKeyType, YourValueType>();
yourVar[YourKeyType] = <YourValueType> yourValue;
Cheers!
干杯!
回答by Hyman Miller
回答by carolopolo
an example of a key value pair is:
键值对的一个例子是:
[key: string]: string
[key: string]: string
you can put anything as the value - if you have programmatically populated values whose types vary, you could put:
您可以将任何内容作为值 - 如果您有以编程方式填充的类型不同的值,您可以输入:
[key: string]: any
[key: string]: any
though proceed at your own discretion with any
:)
尽管您可以自行决定继续any
:)
回答by magikMaker
You can also consider using Record
, like this:
你也可以考虑使用Record
,像这样:
const someArray: Record<string, string>[] = [
{'first': 'one'},
{'second': 'two'}
];
Or write something like this:
或者这样写:
const someArray: {key: string, value: string}[] = [
{key: 'first', value: 'one'},
{key: 'second', value: 'two'}
];
回答by HgMs
class Pair<T1, T2> {
private key: T1;
private value: T2;
constructor(key: T1, value: T2) {
this.key = key;
this.value = value;
}
getKey() {
return this.key;
}
getValue() {
return this.value;
}
}
const myPair = new Pair<string, number>('test', 123);
console.log(myPair.getKey(), myPair.getValue());