The Set object lets you store unique values of any type, whether primitive values or object references.
So this will simply add 2 objects to Set since they do not have the same object references.
var mySet = new Set([{Name: "Ann"}, {Name: "Ann"}]);
console.log(mySet.size) // 2
You might as well use an array in this case.
So in this scenario to remove Ann (since we do not have an object reference) we would have to:
var mySet = new Set([1, 2, "abc", {Name: "Ann"}]);
console.log('With Ann size:', mySet.size)
mySet.forEach(x => x.Name === 'Ann' ? mySet.delete(x) : x)
console.log('Bye Ann size:', mySet.size)
Now consider this:
var ann = {Name: 'Ann'}
var mySet = new Set([ann, ann])
console.log('With Ann size:', mySet.size)
mySet.delete(ann)
console.log('Bye Ann size:', mySet.size)
So as you can see we are now dealing with object references and firstly the size is only 1 and the Set can now find ann reference and remove it with no issues.
The Set object lets you store unique values of any type, whether primitive values or object references.
So this will simply add 2 objects to Set since they do not have the same object references.
var mySet = new Set([{Name: "Ann"}, {Name: "Ann"}]);
console.log(mySet.size) // 2
You might as well use an array in this case.
So in this scenario to remove Ann (since we do not have an object reference) we would have to:
var mySet = new Set([1, 2, "abc", {Name: "Ann"}]);
console.log('With Ann size:', mySet.size)
mySet.forEach(x => x.Name === 'Ann' ? mySet.delete(x) : x)
console.log('Bye Ann size:', mySet.size)
Now consider this:
var ann = {Name: 'Ann'}
var mySet = new Set([ann, ann])
console.log('With Ann size:', mySet.size)
mySet.delete(ann)
console.log('Bye Ann size:', mySet.size)
So as you can see we are now dealing with object references and firstly the size is only 1 and the Set can now find ann reference and remove it with no issues.
If you're able to initialise the object before passing it to Set. And retain reference to is (i.e. scope);
const person = { name: "Ann" };
const my_set = new Set([1, 2, person]);
my_set.has(person); //true
my_set.delete(person);
my_set.has(person); // false
Otherwise;
const my_set = new Set([1, 2, { name: "Ann" }]);
my_set.forEach(item => { if (item.name === "Ann") my_set.delete(item); });
It is much easier to write your own function than use a library for a single lightweight functionality:
var mySet = new Set(["foo", "bar", "baz"]);
var valuesToRemove = new Set(["foo", "baz"]);
function removeAll(originalSet, toBeRemovedSet) {
[...toBeRemovedSet].forEach(function(v) {
originalSet.delete(v);
});
}
console.log([...mySet]);
removeAll(mySet, valuesToRemove);
console.log([...mySet]);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
I have used ES6 syntax since you use ES6, according to your question.
You can this function in a static class like SetUtility for your convenience.
You could use Set#forEach directly with the set and delete then the value from the other set.
var mySet = new Set(["foo", "bar", "baz"]);
var valuesToRemove = new Set(["foo", "baz"]);
function removeAll(originalSet, toBeRemovedSet) {
toBeRemovedSet.forEach(Set.prototype.delete, originalSet);
}
console.log([...mySet]);
removeAll(mySet, valuesToRemove);
console.log([...mySet]);
Run code snippetEdit code snippet Hide Results Copy to answer Expand