搜索
首页web前端前端问答es6怎么找出2个数组中不同项

es6怎么找出2个数组中不同项

Nov 01, 2022 pm 06:07 PM
es6es6数组

步骤:1、将两个数组分别转为set类型,语法“newA=new Set(a);newB=new Set(b);”;2、利用has()和filter()求差集,语法“new Set([...newA].filter(x =>!newB.has(x)))”,差集元素会被包含在一个set集合中返回;3、利用Array.from将集合转为数组类型,语法“Array.from(集合)”。

es6怎么找出2个数组中不同项

本教程操作环境:windows7系统、ECMAScript 6版、Dell G3电脑。

找出2个数组中不同项,就是获取两个数组的差集。

在es6中,可以借助set对象的has()方法来求两个数组的差集。

实现步骤:

步骤1、将两个数组分别转为set类型

let a=[1, 2, 3];
let b=[3, 5, 2];
newA = new Set(a);
newB = new Set(b);

1.png

步骤2:求差集

利用set对象的has() 方法配合数组的filter()来求两个数组的差集。

  • Set has() 方法指示 Set 对象是否包含指定的值;如果指定的值存在,则返回真,否则返回假。

  • 而filter()方法用于过滤数组,返回符合条件(为真)的元素。

实现代码

let a=[1, 2, 3];
let b=[3, 5, 2];
newA = new Set(a);
newB = new Set(b); 
console.log(newA);
console.log(newB);
let differenceABSet = new Set([...newA].filter(x => !newB.has(x)));
console.log("差集为:");
console.log(differenceABSet);

2.png

可以看出此时,差集元素被包含在一个set集合中返回,我们可以将其转为数组类型。

步骤3:利用Array.from方法将集合转为数组类型

let a=[1, 2, 3];
let b=[3, 5, 2];
newA = new Set(a);
newB = new Set(b); 
console.log(newA);
console.log(newB);
let differenceABSet = Array.from(new Set([...newA].filter(x => !newB.has(x))));
console.log("差集为:");
console.log(differenceABSet);

3.png

说明:Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)。

扩展知识:求并集/交集

let a = new Set([1, 2, 3]);
let b = new Set([3, 5, 2]); 

// 并集
let unionSet = new Set([...a, ...b]);
//[1,2,3,5]

// ab交集
let intersectionSet = new Set([...a].filter(x => b.has(x)));

【相关推荐:javascript视频教程web前端

以上是es6怎么找出2个数组中不同项的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
掌握CSS选择器:高效样式的类别与ID掌握CSS选择器:高效样式的类别与IDMay 16, 2025 am 12:19 AM

使用类选择器和ID选择器取决于具体用例:1)类选择器适用于多元素、可重用样式,2)ID选择器适用于唯一元素、特定样式。类选择器更灵活,ID选择器处理速度更快但可能影响代码维护性。

HTML5规范:探索关键目标和动机HTML5规范:探索关键目标和动机May 16, 2025 am 12:19 AM

keykeygoalsandmotivationsbehindhtml5weretoenhancesemantstructure,Improvemultimediasupport,andensureBetterperformanceandCompatibalityAcroscaroscaroscaroscarossdecrossdecrossdecrossdecrossdecrossdecrossdecrossdevices,drivendybytheneedtoAddresshtml4'slimitationsand limitiTations and limittations andmeetmeetModerntructAndmmoderntructss.1)

CSS ID和类:简单指南CSS ID和类:简单指南May 16, 2025 am 12:18 AM

IDSareNiqueAndusedForsingLelement,andleclassEsareReusableFormultPirultElements.1)useIdIdSforuniqueElementsLikeAspeCificheader.2)useclassesforconsistentSistentSistentStyActStyAcroSsmultipleLementslike.3)becautiouswithspecificitificitieAsideCerrrase.4)

HTML5目标:了解规范的关键目标HTML5目标:了解规范的关键目标May 16, 2025 am 12:16 AM

html5aimstoenhancewebaccctible,互动性和效率。1)ITSupportsMultimediawithOutPlugins,Simplifyinginguserexperience.2)Semanticmarkmarksmarkupimprovissupimprovessupstructureandacccessessible.3)增强bacegencementingIncrassubility.4)

使用HTML5难以实现其目标吗?使用HTML5难以实现其目标吗?May 16, 2025 am 12:06 AM

html5isnotparticulllydifficulttousebutrequirequireSustingingItsFeatures.1)smanticelementslike like ,,,和iMproveructure,andimprovucture,可读性,seo和acctibility.2)多中性倍增量,且可读性

CSS:我可以在同一DOM中使用多个ID吗?CSS:我可以在同一DOM中使用多个ID吗?May 14, 2025 am 12:20 AM

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

HTML5的目的:创建一个更强大,更容易访问的网络HTML5的目的:创建一个更强大,更容易访问的网络May 14, 2025 am 12:18 AM

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互动,可及可访问。1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

HTML5的重要目标:增强网络开发和用户体验HTML5的重要目标:增强网络开发和用户体验May 14, 2025 am 12:18 AM

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒体综合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityAncccossibility.2)和TagsallowsemplowsemplowseamemelesseamlessallowsemlessemlessemelessmultimedimeDiaiiaemediaiaembedwitWithItWitTplulurugIns.3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)