To remove a specific item from a set in JavaScript, you can use the delete
keyword.
In this example,
numberSet
with values const numberSet = new Set([1, 2, 3, 4, 5]);
.delete
keyword to remove the item '2' from numberSet
.delete
keyword removes the specified item from the set, if it exists.const numberSet = new Set([1, 2, 3, 4, 5]);
console.log('Original Set:', numberSet);
numberSet.delete(2);
console.log('Set after removing item 2:', numberSet);
Original Set: Set(5) { 1, 2, 3, 4, 5 } Set after removing item 2: Set(4) { 1, 3, 4, 5 }
In this example,
stringSet
with values const stringSet = new Set(['apple', 'banana', 'orange']);
.delete
keyword to remove the string 'banana' from stringSet
.delete
keyword removes the specified item from the set, if it exists.const stringSet = new Set(['apple', 'banana', 'orange']);
console.log('Original Set:', stringSet);
stringSet.delete('banana');
console.log('Set after removing 'banana':', stringSet);
Original Set: Set(3) { 'apple', 'banana', 'orange' } Set after removing 'banana': Set(2) { 'apple', 'orange' }
In this tutorial, we learned How to Remove Specific Item from a Set in JavaScript language with well detailed examples.