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() 方法从该 map 中删除指定键的元素。

尝试一下

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

console.log(map1.delete("bar"));
// Expected result: true
// True indicates successful removal

console.log(map1.has("bar"));
// Expected result: false

语法

js
mapInstance.delete(key)

参数

key

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

返回值

如果 Map 对象中的元素存在并已被移除,则为 true;如果该元素不存在,则为 false

示例

使用 delete()

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

console.log(myMap.delete("bar")); // 返回 true。成功地移除元素
console.log(myMap.has("bar")); // 返回 false。"bar" 元素将不再存在于 Map 实例中

规范

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

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
delete

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见