どうも、ちょげ(@chogetarou)です。
Map(マップ)の全てのキー(Key)を取得する方法を紹介します。
方法

Map(マップ)の全てのキーを取得するには、keys()を使います。
具体的な方法としては、「map.keys()
」のように、Mapからkeys()を呼び出します。
//T=キーの型、map=マップ
const keys: IterableIterator<T> = map.keys()
上記のkeys()は、呼び出したMapから全てのキーを取得します。
使用例
const nums: Map<string, number> = new Map();
nums.set("one", 1)
nums.set("two", 2)
nums.set("three", 3)
nums.set("four", 4)
nums.set("five", 5)
const keys: IterableIterator<string> = nums.keys()
for (let key of keys) {
console.log(key)
}
出力:
[LOG]: "one"
[LOG]: "two"
[LOG]: "three"
[LOG]: "four"
[LOG]: "five"
コメント