Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Set.prototype.delete()

BaselineWidely available

Thedelete() method ofSet instances removes a specified value from this set, if it is in the set.

Try it

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

Syntax

js
setInstance.delete(value)

Parameters

value

The value to remove fromSet.

Return value

Returnstrue ifvalue was already inSet; otherwisefalse.

Examples

Using the delete() method

js
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.

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);  }});

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-set.prototype.delete

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp