Set.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.
Set
实例的 delete()
方法会从此集合中删除指定值(如果该值在集合中)。
¥The delete()
method of Set
instances removes a specified value from this set, if it is in the set.
Try it
语法
参数
返回值
示例
使用 delete()方法
从集合中删除对象
¥Deleting an object from a set
由于对象是通过引用进行比较的,因此如果没有对原始对象的引用,则必须通过检查各个属性来删除它们。
¥Because objects are compared by reference, you have to delete them by checking individual properties if you don't have a reference to the original object.
js
const setObj = new Set(); // Create a new set.
setObj.add({ x: 10, y: 20 }); // Add object in the set.
setObj.add({ x: 20, y: 30 }); // Add object in the set.
// Delete any point with `x > 10`.
setObj.forEach((point) => {
if (point.x > 10) {
setObj.delete(point);
}
});
规范
Specification |
---|
ECMAScript Language Specification # sec-set.prototype.delete |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also