➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
How to Remove from Set JavaScript | The set.delete() method in JavaScript deletes an element in a set with a supplied value and returns a boolean value based on the element’s availability. If the element with the supplied value does not exist in the set, it will yield false instead of modifying it. This article will give you an insight into how to remove an item from the set in JavaScript using different methods. Let us have a look at the following examples.
Example 1:
set = [cat, dog, pig, fish]
Remove “fish” from the set
Set after removing the element:-
[cat, dog, pig]
Example 2:
set = [one, two, three, four, ten]
Remove “two” from the set
Set after removing the element:-
[one, three, four, ten]
The syntax for the delete() function is:- set1.delete(e)
The delete() method is used to remove/delete a particular piece e from a Set “set1”.
Let us just look at some illustrations of the delete() method in action to remove from the set in JavaScript:-
Approach to remove from set JavaScript:–
1. Initialize a new string set using set() method and add new elements to it using the add() function.
2. Print the set.
3. Use the myset.delete() function to remove an element from set in JavaScript.
4. The set without the deleted element will be printed.
// initialize a new set using Set()
let myset = new Set();
// add new elements to the set using add()
myset.add(8993);
myset.add(123);
// print the set
console.log(myset);
// Since 8993 exists,
// it will be removed and it will return true
console.log(myset.delete(8993));
// Since 9999 doesn't exists,
// Hence it will return false
console.log(myset.delete(9999));
// print the set
console.log(myset);
Output:-
Set(2) { 8993, 123 }
true
false
Set(1) { 123 }
JavaScript Remove From Set using Function
Another way in which we can remove item from set in JavaScript is by creating our own function. We will define a function to remove a specific item from the given set.
var mySet = new Set(["PHP", "C++", "Python", "JavaScript"]);
var valuesRemove = new Set(["C++"]);
function removeAll(originalSet, toBeRemovedSet) {
[...toBeRemovedSet].forEach(function (v) {
originalSet.delete(v);
});
}
console.log([...mySet]);
removeAll(mySet, valuesRemove);
console.log([...mySet]);
Output:-
[ ‘PHP’, ‘C++’, ‘Python’, ‘JavaScript’ ]
[ ‘PHP’, ‘Python’, ‘JavaScript’ ]
Method-3 to remove item from set JavaScript
Array.prototype.remByVal = function (val) {
for (var i = 0; i < this.length; i++) {
for (var j = 0; j < val.length; j++) {
if (this[i] === val[j]) {
this.splice(i, 1);
i--;
}
}
}
return this;
}
var set = ["Mango", "Apple", "Orange", "Mark", "Banana", "Guava",];
var remove = ["Mark"];
set = set.remByVal(remove);
console.log(set);
Output:-
[ ‘Mango’, ‘Apple’, ‘Orange’, ‘Banana’, ‘Guava’ ]
This brings us to the end of this article, we hope you learned how to remove from set JavaScript. Also see:- Javascript Remove Last Digit from Number
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!