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 2015年7月.
delete()
方法會一個 Set
物件中移除指定元素。
嘗試一下
const set1 = new Set();
set1.add({ x: 10, y: 20 }).add({ x: 20, y: 30 });
// Delete any point with `x > 10`.
set1.forEach((point) => {
if (point.x > 10) {
set1.delete(point);
}
});
console.log(set1.size);
// Expected output: 1
語法
js
mySet.delete(value);
參數'
value
-
要從
Set
物件中移除的值。
回傳值
true
如果成功從 Set
物件中移除;反之 false
。
範例
>使用 delete
方法
js
var mySet = new Set();
mySet.add("foo");
mySet.delete("bar"); // Returns false. No "bar" element found to be deleted.
mySet.delete("foo"); // Returns true. Successfully removed.
mySet.has("foo"); // Returns false. The "foo" element is no longer present.
下方展示了如何從一個 Set 中移除物件。
js
var 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(function (point) {
if (point.x > 10) {
setObj.delete(point);
}
});
規範
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-set.prototype.delete> |
瀏覽器相容性
Loading…