[ES2024] Perform Set Operations using JavaScript Set Methods
The "Set Methods" proposal introduces 7 new methods to JavaScript/ECMAScript:
-
union(setB): Returns a new
Set
containing all elements from the original set and another setsetB
. This method effectively combines the members of two sets without duplication. -
intersection(setB): Returns a new
Set
containing all elements that are present in both the original set and setsetB
. This method identifies common elements between two sets. -
difference(setB): Returns a new
Set
containing all elements that are in the original set but not in setsetB
. It helps in identifying elements that are unique to the first set when compared to another set. -
symmetricDifference(setB): Returns a new
Set
containing all elements that are in either of the sets but not in their intersection. This method is useful for finding elements that are in either one set or the other, but not in both. -
isSubsetOf(setB): Returns a boolean indicating whether the original set is a subset of set
setB
. A set is a subset of another set if all elements of the former are contained in the latter. -
isSupersetOf(setB): Returns a boolean indicating whether the original set is a superset of set
setB
. A set is a superset of another set if it contains all elements of the latter set. -
isDisjointFrom(setB): Returns a boolean indicating whether the original set and set
setB
have no elements in common. If they have no common elements, the sets are considered disjoint.
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
// Using union to combine two sets
const unionSet = setA.union(setB);
console.log('Union of setA and setB:', [...unionSet]);
// Using intersection to find common elements
const intersectionSet = setA.intersection(setB);
console.log('Intersection of setA and setB:', [...intersectionSet]);
// Using difference to find elements in setA not in setB
const differenceSet = setA.difference(setB);
console.log('Difference of setA from setB:', [...differenceSet]);
// Using symmetricDifference to find elements in either set but not in both
const symmetricDifferenceSet = setA.symmetricDifference(setB);
console.log('Symmetric Difference between setA and setB:', [...symmetricDifferenceSet]);
// Checking if setA is a subset of setB
const isSubset = setA.isSubsetOf(setB);
console.log('Is setA a subset of setB?', isSubset);
// Checking if setA is a superset of setB
const isSuperset = setA.isSupersetOf(setB);
console.log('Is setA a superset of setB?', isSuperset);
// Checking if setA is disjoint from setB
const isDisjoint = setA.isDisjointFrom(setB);
console.log('Are setA and setB disjoint?', isDisjoint);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2022-05-20 [Spring Data JPA] Custom Query
2021-05-20 [AWS] ElasticSearch, Sync data between DynamoDB and ElasticSearch by using DyanmoDB Stream
2020-05-20 [Functional Programming] EitherToTask transform
2020-05-20 [Functional Programming] Traverse Task and Either
2019-05-20 [AngularJS] Decorator pattern for code reuse
2019-05-20 [Functional Programming] Async ADT
2016-05-20 [AWS S3] Hosting a Static Website on Amazon S3