Anahtar, değer çifti typcript'te mevcut mu? Cevabınız evet ise nasıl yapılır. Herkes örnek örnek bağlantılar sağlayabilir.
Anahtar, değer çifti typcript'te mevcut mu? Cevabınız evet ise nasıl yapılır. Herkes örnek örnek bağlantılar sağlayabilir.
Yanıtlar:
Anahtar / değer çifti Typescript'te mevcut mu?
Evet. Bir dizin imzası olarak adlandırıldı :
interface Foo {
[key: string]: Bar;
}
let foo:Foo = {};
İşte anahtarlar stringve değerler Bar.
Bir ES6 kullanabilirsiniz Mapuygun sözlüklerde için tarafından polyfilledcore-js .
key. Sen, mesela, aynı zamanda bu yazabilirsiniz: [countryCode: string]: string. Okunabilirlik için güzel.
En basit yol şöyle bir şey olabilir:
var indexedArray: {[key: string]: number}
Kullanımı:
var indexedArray: {[key: string]: number} = {
foo: 2118,
bar: 2118
}
indexedArray['foo'] = 2118;
indexedArray.foo= 2118;
let foo = indexedArray['myKey'];
let bar = indexedArray.myKey;
Bar
Anahtar / değer çifti Typescript'te mevcut mu?
Bir C # KeyValuePair <string, string> düşünürseniz : Hayır, ancak kendiniz kolayca bir tane tanımlayabilirsiniz:
interface KeyValuePair {
key: string;
value: string;
}
Kullanımı:
let foo: KeyValuePair = { key: "k", value: "val" };
KeyValuePairbir liste değildir, ancak bir liste girişi olabilir. Belki bir List<>arıyorsunuz? Stackoverflow.com/questions/23096260/… sayfasına
Soru soran için değil, ilgilenen diğerleri için: Bkz: Anahtar-değer çiftinin Typescript Haritası nasıl tanımlanır. burada anahtar bir sayıdır ve değer bir nesneler dizisidir
Çözüm bu nedenle:
let yourVar: Map<YourKeyType, YourValueType>;
// now you can use it:
yourVar = new Map<YourKeyType, YourValueType>();
yourVar[YourKeyType] = <YourValueType> yourValue;
Şerefe!
Maptanımlanır? Açısal özel mi?
Diğer bir basit yol, bir demet kullanmaktır :
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10];
// Access elements
console.log("First: " + x["0"] + " Second: " + x["1"]);
Çıktı:
İlk: merhaba İkinci: 10
Ayrıca şu şekilde kullanmayı da düşünebilirsiniz Record:
const someArray: Record<string, string>[] = [
{'first': 'one'},
{'second': 'two'}
];
Veya bunun gibi bir şey yazın:
const someArray: {key: string, value: string}[] = [
{key: 'first', value: 'one'},
{key: 'second', value: 'two'}
];
anahtar-değer çifti örneği:
[key: string]: string
değer olarak herhangi bir şey koyabilirsiniz - türleri değişen, programlı olarak doldurulmuş değerleriniz varsa, şunu koyabilirsiniz:
[key: string]: any
yine de kendi takdirinize göre devam edin any:)
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());
Kısa bir yol, bir demeti anahtar / değer çifti olarak kullanmaktır :
const keyVal: [string, string] = ["key", "value"] // explicit type
// or
const keyVal2 = ["key", "value"] as const // inferred type with const assertion
[key, value] demetler ayrıca JS yerleşik nesnelerine uyumluluk sağlar:
Object, özellikle. Object.entries,Object.fromEntriesMap, özellikle. Map.prototype.entriesve new Map()kurucuSet, özellikle. Set.prototype.entriesYeniden KeyValuePairkullanılabilirlik için genel bir tür oluşturabilirsiniz :
type KeyValuePair<K extends string | number, V = unknown> = [K, V]
const kv: KeyValuePair<string, string> = ["key", "value"]
Yaklaşan TS 4.0, daha iyi dokümantasyon ve takım desteği için adlandırılmış / etiketlenmiş demetler sağlar:
type KeyValuePairNamed = [key: string, value: string] // add `key` and `value` labels
const [key, val]: KeyValuePairNamed = ["key", "val"] // array destructuring for convenience