当前位置:网站首页>ES6 how to find the intersection of two arrays
ES6 how to find the intersection of two arrays
2022-04-21 21:48:00 【Billion speed cloud】
es6 How to find the intersection of two arrays
This article mainly introduces “es6 How to find the intersection of two arrays ”, In daily operation , I believe a lot of people are es6 There are doubts about how to find the intersection of two arrays , Xiao Bian consulted all kinds of materials , Sort out simple and easy-to-use operation methods , I hope to answer ”es6 How to find the intersection of two arrays ” Your doubts help ! Next , Please follow Xiaobian to learn !
Implementation method :1、 Use “new Set( Array )” Statement converts both arrays to set Collection types ;2、 Use “new Set([... aggregate 1].filter(x => aggregate 2.has(x)))” Statement to get the intersection of two sets , A new set containing all intersection elements will be returned .
The operating environment of this tutorial :windows7 System 、ECMAScript 6 edition 、Dell G3 The computer .
stay es6 in , You can use set Object's has() Method with the array of filter() To find the intersection of two arrays .
Set yes ES6 New data structure , It's like an array , But there are no duplicate values in itself . Take advantage of this feature , We can turn the array into Set Type for de duplication , And then use Array.from Method to convert it into an array .
Set has() Method instructions Set Whether the object contains the specified value . If the specified value exists , Then return to true , Otherwise return false .
let a=[1, 2, 3];let b=[3, 5, 2];newA = new Set(a);newB = new Set(b); let intersectionSet = new Set([...newA].filter(x => newB.has(x)));console.log(intersectionSet);

And you can see that at this point , Intersection elements are contained in a set Returns... In the collection , available Array.from Method to convert a collection to an array type
Array.from Method to convert two types of objects into real arrays : Array like objects (array-like object) Ergodic (iterable) The object of ( Include ES6 New data structure Set and Map).
let intersectionSet = Array.from(new Set([...newA].filter(x => newB.has(x))));console.log(intersectionSet);

Expanding knowledge : Union / Difference set
let a = new Set([1, 2, 3]);let b = new Set([3, 5, 2]); // Combine let unionSet = new Set([...a, ...b]);//[1,2,3,5]// ab Difference set let differenceABSet = new Set([...a].filter(x => !b.has(x)));
Here we are , About “es6 How to find the intersection of two arrays ” That's the end of my study , I hope we can solve your doubts . The combination of theory and practice can better help you learn , Let's try ! If you want to continue to learn more related knowledge , Please continue to pay attention to Yisu cloud website , Xiaobian will continue to strive to bring you more practical articles !
版权声明
本文为[Billion speed cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212147080826.html
边栏推荐
- Alibaba cloud OSS, user authentication and patient
- What are the functions of ports 135, 137, 138, 139 and 445 of TCP and UDP?
- UML integrated design example
- 在线YAML转Properties工具
- 【FeignClient】feignClient跨服务下载文件
- Successful cases of project management system development
- 自动生成排除某个字段的sql
- Live555 learning
- Redis+Caffeine两级缓存,让访问速度纵享丝滑
- Mongo geonear query Chinese fuzzy search in PHP
猜你喜欢

一加连发两款耳机产品:充电10分钟 听歌20小时

硬核实力,多方认可|云扩科技作为RPA核心厂商入选《2022中国RPA采购指南》

外包学生管理系统的架构文档

Operation instructions of training management system

什么?你们公司还没有将JVM初始和最大堆内存大小设置为相同值?

Tgip-cn 038 registration | in depth analysis of Apache pulsar source code reading correct posture (I)
![[charming Java] - data types and variables](/img/d1/22122803b7b796fbc5e269219d6db8.png)
[charming Java] - data types and variables

Use try-with-resources or close this “FileOutputStream“

LeetCode琅琊榜第三层-盛最多水的容器

还清90万安居贷才能拿到离职证明?腾讯回应:与实际情况不符
随机推荐
Seven schemes of implementing distributed lock based on redis
Online CSV to yaml tool
还清90万安居贷才能拿到离职证明?腾讯回应:与实际情况不符
The @ select annotation is used in the mapper of mybtais, and the if annotation is used
想要从数字化转型获得收益,你该怎么做?
一加连发两款耳机产品:充电10分钟 听歌20小时
jupyter-notebook没有运行按钮
Workflow engine parameter setting and business engine
During iPhone test, the picture of custom tabbar will slide along the screen
FFmpeg连载2-分离视频和音频
Helm install Jenkins (official)
IDEA通过Jedis操作Linux上的Redis;Failed to connect to any host resolved for DNS name问题
Online yaml to properties tool
自动生成排除某个字段的sql
Alibaba cloud OSS, user authentication and patient
avformat_ new_ Stream understanding
Ffmpeg serial 3-video decoding
【迷人的爪哇】——数据类型和变量
分析师认为三星Galaxy Z Fold 4和Z Flip 4可能比其前代产品更便宜
docker MySQL定时备份