Map.prototype.delete()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Map 实例的 delete() 方法通过键从该映射中删除指定元素。

¥The delete() method of Map instances removes the specified element from this map by key.

Try it

语法

¥Syntax

js
mapInstance.delete(key)

参数

¥Parameters

key

要从 Map 对象中删除的元素的键。

返回值

¥Return value

如果 Map 对象中的某个元素存在并已被删除,则返回 true;如果该元素不存在,则返回 false

¥true if an element in the Map object existed and has been removed, or false if the element does not exist.

示例

¥Examples

使用删除()

¥Using delete()

js
const myMap = new Map();
myMap.set("bar", "foo");

console.log(myMap.delete("bar")); // Returns true. Successfully removed.
console.log(myMap.has("bar")); // Returns false. The "bar" element is no longer present.

规范

Specification
ECMAScript Language Specification
# sec-map.prototype.delete

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also