1

I have a set for which I want to compare if it contains all the values or not.

const mySet = new Set([1,2,3,4,5])

This is what I want

const isAllActive = mySet.has(1) && mySet.has(2) && mySet.has(3) && mySet.has(4) && mySet.has(5);

am sure instead of this repetition there must be some other way to check that.

Udit_1
  • 145
  • 1
  • 1
  • 6
  • Does this answer your question? [comparing ECMA6 sets for equality](https://stackoverflow.com/questions/31128855/comparing-ecma6-sets-for-equality) – user120242 May 13 '20 at 23:46
  • 2
    `[1,2,3,4,5].every(mySet.has)` (note that this question ultimately isn't about sets or comparing them, it's about calling a function with a bunch of arguments and compiling the results; solutions are easier to find if you try to generalize the problem as much as possible) –  May 13 '20 at 23:47

1 Answers1

2

One option is to use the every array method to see if every element in the array you're testing is in the Set.

const mySet = new Set([1,2,3,4,5]);
const hasAll = [1, 2, 3, 4, 5].every(el => mySet.has(el));

console.log(hasAll);
Nick
  • 16,066
  • 3
  • 16
  • 32