Morgan Courbet
Morgan Courbet

Reputation: 911

Remove set of values in an existing Set

In JavaScript, I have the following Sets:

var mySet = new Set(["foo", "bar", "baz"]);
var valuesToRemove = new Set(["foo", "baz"]);

I need a function that removes a set of values:

removeAll(mySet, valuesToRemove);
// expected value of mySet: Set ["bar"]

// or a function that returns a new Set
var myNewSet = removeAll(mySet, valuesToRemove);
// expected value of myNewSet: Set ["bar"]

Question: Does any ES6-modules-compatible library exists that does the trick?

Lodash has a similar function for Arrays, but supporting JavaScript builtin iterable is tagged as wontfix. And I prefer to avoid doing multiple Array↔Set/Set↔Array conversions.

Alternatively, I will write my own function.

Upvotes: 10

Views: 17751

Answers (5)

NKSM
NKSM

Reputation: 5874

The difference() method of a Set instance takes another set as an argument and returns a new set containing elements that are present in the original set but not in the specified set.

So the difference() method could be used here:

var mySet = new Set(["foo", "bar", "baz"]);
var valuesToRemove = new Set(["foo", "baz"]);

const cleanedSet = mySet.difference(valuesToRemove);
console.log(cleanedSet) > Output: Set(1) {'bar'}

Source: Set.prototype.difference()

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386680

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

Upvotes: 4

Muhammad Awais
Muhammad Awais

Reputation: 4502

I think this will work try this,

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 rooms = [1,2,3,4,5,6,7,8,9]
var re= [1,4,6,7,9]
rooms = rooms.remByVal(re)

console.log(rooms)

Upvotes: -2

guest271314
guest271314

Reputation: 1

You can use for..of loop .delete()

var removeAll = (keys, arr) => {for (prop of keys) arr.delete(prop); return arr};
removeAll(valuesToRemove, mySet); // `Set [ "bar" ]`

Upvotes: 2

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34234

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

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.

Upvotes: 8

Related Questions