首页 > 编程语言 >[Javascript] Perform Set Operations using JavaScript Set Methods

[Javascript] Perform Set Operations using JavaScript Set Methods

时间:2024-05-20 22:09:27浏览次数:25  
标签:Operations set const setA setB JavaScript Set elements

The "Set Methods" proposal introduces 7 new methods to JavaScript/ECMAScript:

  1. union(setB): Returns a new Set containing all elements from the original set and another set setB. This method effectively combines the members of two sets without duplication.

  2. intersection(setB): Returns a new Set containing all elements that are present in both the original set and set setB. This method identifies common elements between two sets.

  3. difference(setB): Returns a new Set containing all elements that are in the original set but not in set setB. It helps in identifying elements that are unique to the first set when compared to another set.

  4. 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.

  5. 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.

  6. 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.

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

 

标签:Operations,set,const,setA,setB,JavaScript,Set,elements
From: https://www.cnblogs.com/Answer1215/p/18202898

相关文章

  • what's the advantages of using Map over Object in JavaScript?
    what'stheadvantagesofusingMapoverObjectinJavaScript?在JavaScript中使用Map相对于Object有什么优势?prosconsdemoshttps://leetcode.com/studyplan/30-days-of-javascript/(......
  • 解决yarn打包时出现“FATAL ERROR: Reached heap limit Allocation failed - JavaScri
    1、......
  • [LeetCode] 1863. Sum of All Subset XOR Totals
    TheXORtotalofanarrayisdefinedasthebitwiseXORofallitselements,or0ifthearrayisempty.Forexample,theXORtotalofthearray[2,5,6]is2XOR5XOR6=1.Givenanarraynums,returnthesumofallXORtotalsforeverysubsetofnums.......
  • JavaScript------querySelector/querySelectorAll的使用
    1、基础语法querySelector()方法返回文档中匹配指定CSS选择器的一个元素。querySelector()方法仅仅返回匹配指定选择器的第一个元素。如果你需要返回所有的元素,请使用querySelectorAll()方法替代。属性:指定一个或多个匹配元素的CSS选择器。可以使用它们的id,类,类......
  • mongo replicaset=rs0 com.mongodb.MongoSocketException: centosc
    1、描述虚拟机搭建mongo副本集虚拟机的设定的hostname为: centosc。虚拟机IP为192.168.25.129搭建三个副本集端口分别为,28017、28018、28019,运行mongo副本集报错,报错信息如下:2024-05-2010:22:39:235[main]INFOorg.apache.coyote.http11.Http11NioProtocol-StartingProtocol......
  • setting配置
    Setting配置信息注册子应用#注意:需要修改Django的全局配置文`settings.py`'''1.创建子应用1)在pycharm中创建pythonmanage.pystartapp子应用名2)在cmd命令中创建django-adminstartapp子应用名2.在INSTALLED_APPS配置中写入创建好的子应用名......
  • redis数据结构:RedisObject,SkipList,SortedSet
    1.RedisObject对象redis中任何KV都会被封装为RedisObject对象,也叫做Redis对象 2.SkipList跳表元素按照升序排列存储,是有序的双向链表节点可以有多个指针,并且跨度不同。指针个数根据节点数自动生成,1~32性能和红黑树;二分查找差不多。实现简单,但是空间复杂度高样例:1——2......
  • pytorch 转 tensorRT 踩的几个小坑_tensorrt engine set up failed
    CSDN搬家失败,手动导出markdown后再导入博客园1、版本不匹配[E][TRT]Layer:Where_51'soutputcannotbeusedasshapetensor.[E][TRT]Networkvalidationfailed.[E]Enginecreationfailed.[E]Enginesetupfailed.这实际是由于pytorch与TensorRT版本不匹......
  • JavaScript Promise Time Limit Function All In One
    JavaScriptPromiseTimeLimitFunctionAllInOneLeetCode2637.PromiseTimeLimiterrorsfunctiontimeLimit(fn:Fn,t:number):Fn{returnasyncfunction(...args){//letbegin=Date.now();letbegin=performance.now();letresult=......
  • [MASM拾遗]Offset
      Offset伪指令我一直都认为只是获取标识符在段中的偏移地址,但经研究,发现了部分违反直觉的细微区别:  1、在完整端声明(Fullsegmentdefinition)的情况下,如果offsetmygroup:myvar或offsetmysegment:myvar,可通过端前缀来获取myvar与group开头位置的偏移地址或myvar与mysegme......