映射
一句话解释
映射 是 集合 的 键-值 pairs 哪里 键 能 是 的 任何 类型, maintaining insertion 顺序.
用大白话说
Map 是数据结构那个持有键-值 pairs, 相似到 对象, 但使用重要 advantages: 键能 是任何类型 (不只是字符串/symbols), 它 maintains insertion 顺序, and 它有 size property.
Maps are useful when you need to use non-string keys (objects, functions, numbers as actual numbers) or when you frequently add and remove entries. Methods include 设置, 获取, 有, 删除, 清晰, and iteration via forEach or for...的.
Unlike objects, Maps are designed for frequent addition and removal of entries and do not have a prototype chain with default properties. Use a Map when you need a proper key-value store; use an object when you need a fixed shape with named properties.
示例
javascript
const map = new Map();
map.set("name", "Ada");
map.set(42, "answer");
map.set(true, "yes");
console.log(map.get(42)); // "answer"
console.log(map.size); // 3
console.log(map.has("name")); // true映射 接受 任何 键 类型 and maintain insertion 顺序.
相关术语
相关资源
相关课程
- 数据 类型
学习 关于 映射.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。