The delete()
method removes a specified value from a Set
object, if it is in the set.
The delete()
method removes a specified value from a Set
object, if it is in the set.
delete(value)
value
The value to remove from Set
.
Returns true
if value
was already in Set
; otherwise false
.
const mySet = new Set(); mySet.add("foo"); console.log(mySet.delete("bar")); // false; no "bar" element found to be deleted. console.log(mySet.delete("foo")); // true; successfully removed. console.log(mySet.has("foo")); // false; the "foo" element is no longer present.
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.
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); } });
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
delete |
38 |
12 |
13 |
11 |
25 |
8 |
38 |
38 |
14 |
25 |
8 |
3.0 |
1.0 |
0.12.0
0.10.0
|
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/delete