JavaScript Map has()
定义和用法
has() 方法用于检查 Map 中是否存在某个键,如果存在则返回 true。
实例
例子 1
// 创建一个 Map
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
// Map 中是否有 "apples"?
fruits.has("apples");
例子 2
尝试以下操作:
fruits.delete("apples");
fruits.has("apples");
语法
map.has(value)参数
| 参数 | 描述 |
|---|---|
| value | 必需。要检查的键。 |
返回值
| 类型 | 描述 |
|---|---|
| Boolean | 如果键存在则返回 true,否则返回 false。 |
浏览器支持
map.has() 是 ECMAScript6 (ES6) 的特性。
自 2017 年 6 月起,所有现代浏览器均支持 ES6(JavaScript 2015):
| Chrome | Edge | Firefox | Safari | Opera |
|---|---|---|---|---|
| Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
| 2016 年 5 月 | 2017 年 4 月 | 2017 年 6 月 | 2016 年 9 月 | 2016 年 6 月 |
map.has() 在 Internet Explorer 中不受支持。