多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# TypeScript 映射 > 原文: [https://howtodoinjava.com/typescript/maps/](https://howtodoinjava.com/typescript/maps/) **映射**是 ES6 中引入的新数据结构。 它允许您存储类似于其他编程语言的键值对,例如 Java,C#。 ## 建立映射 使用`Map`类型和`new`关键字在 TypeScript 中创建`Map`。 ```java let myMap = new Map(); ``` ## 从映射添加/检索/删除条目 1. `map.set()` – 在`Map`中添加条目的方法。 2. `map.get()` – 从`Map`检索条目。 3. `map.has()` – 在 `Map`中存在条目。 4. `map.delete()` – 从`Map`删除条目。 5. `map.size` – 将返回`Map`的大小。 ```java let nameAgeMapping = new Map(); //Set entries nameAgeMapping.set("Lokesh", 37); nameAgeMapping.set("Raj", 35); nameAgeMapping.set("John", 40); //Get entries nameAgeMapping.get("John"); //40 //Check entry is present or not nameAgeMapping.has("Lokesh"); //true nameAgeMapping.has("Brian"); //false //Size of Map with nameAgeMapping.size; //3 //Delete an entry nameAgeMapping.delete("Lokesh"); // true //Clear whole Map nameAgeMapping.clear(); //Clear all entries ``` ## 遍历`Map`条目 使用`'for...of'`循环可迭代映射键,值或条目。 ```java let nameAgeMapping = new Map(); nameAgeMapping.set("Lokesh", 37); nameAgeMapping.set("Raj", 35); nameAgeMapping.set("John", 40); //Iterate over map keys for (let key of nameAgeMapping.keys()) { console.log(key); //Lokesh Raj John } //Iterate over map values for (let value of nameAgeMapping.values()) { console.log(value); //37 35 40 } //Iterate over map entries for (let entry of nameAgeMapping.entries()) { console.log(entry[0], entry[1]); //"Lokesh" 37 "Raj" 35 "John" 40 } //Using object destructuring for (let [key, value] of nameAgeMapping) { console.log(key, value); //"Lokesh" 37 "Raj" 35 "John" 40 } ``` 将我的问题放在评论部分。 学习愉快!