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.

Phương thức delete() của các instance Map xóa mục được chỉ định bởi key khỏi Map này.

Try it

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

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

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

Cú pháp

js
mapInstance.delete(key)

Tham số

key

Key của mục cần xóa khỏi đối tượng Map. Các key là đối tượng được so sánh theo tham chiếu, không phải theo giá trị.

Giá trị trả về

true nếu một mục trong đối tượng Map đã được xóa thành công. false nếu key không tìm thấy trong Map.

Ví dụ

Sử dụng 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.

Đặc tả kỹ thuật

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

Tương thích trình duyệt

Xem thêm