简体   繁体   中英

How can I compare values of sets in js

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM