偵測步驟:1、將兩個數組都轉換為set類型,語法「new Set(arr)」;2、取得兩個集合的交集,語法「new Set([...set1].filter (x=>set2.has(x)))”,交集元素會被包含在一個set集合中返回;3、將包含交集元素的set集合轉為數組類型,語法“Array.from(rse)” ;4.判斷交集數組是否為空數組,如果是則沒有相同項,反之則有相同項。
本教學操作環境:windows7系統、ECMAScript 6版、Dell G3電腦。
偵測兩個陣列是否有相同項,就是偵測兩個陣列是否有交集。
偵測想法:
取得兩個陣列的交集
判斷該交集是否為空,如果為空則沒有相同項,如果不為空則有相同項。
實作步驟:
#1、取得兩個陣列的交集
在es6中,可以利用set物件的has() 方法來配合陣列的filter()來求兩個陣列的交集。
Set是ES6新提供的資料結構,類似數組,但是本身沒有重複值。利用這個特性,我們可以將陣列轉換為Set類型進行去重,然後使用Array.from方法將其再轉為陣列。
Set has() 方法指示 Set 物件是否包含指定的值。如果指定的值存在,則傳回真,否則傳回假。
註:想要使用has()方法,需要先將陣列轉換成set集合類型
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);
可以看出此時,交集元素被包含在一個set集合中傳回
2、將包含交集元素的set集合轉為陣列類型
在es6中,可以利用Array.from方法將集合轉換為陣列類型
Array.from方法用於將兩類物件轉換為真正的陣列:類似陣列的物件(array-like object)和可遍歷(iterable)的物件(包括ES6 新增的資料結構Set 和Map)。
let intersectionSet = Array.from(new Set([...newA].filter(x => newB.has(x)))); console.log(intersectionSet);
3、判斷交集數組是否為空數組
如果為空數組,則沒有相同項目
如果不為空數組,則有相同項目
if(intersectionSet==[]){ console.log("没有相同项"); }else{ console.log("有相同项"); }
完整程式碼範例:
let a=[1, 2, 3]; let b=[3, 5, 2]; console.log(a); console.log(b); newA = new Set(a); newB = new Set(b); let intersectionSet = Array.from(new Set([...newA].filter(x => newB.has(x)))); console.log("两个数组的交集:"); console.log(intersectionSet); if(intersectionSet==[]){ console.log("没有相同项"); }else{ console.log("有相同项"); }
【相關推薦:javascript影片教學、程式設計影片】
#以上是es6怎麼偵測兩個陣列是否有相同項的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

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

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒體綜合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityActibility.2)and tagsallowsemlessallowseamelesseamlessallowseamelesseamlesseamelesseamemelessmultimedimeDiaiaembediiaembedplugins.3)。 3)3)

html5isnotinerysecure,butitsfeaturescanleadtosecurityrisksifmissusedorimproperlyimplempled.1)usethesand andboxattributeIniframestoconoconoconoContoContoContoContoContoconToconToconToconToconToconTedContDedContentContentPrenerabilnerabilityLikeClickLickLickLickjAckJackJacking.2)

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

使用ID選擇器在CSS中並非固有地不好,但應謹慎使用。 1)ID選擇器適用於唯一元素或JavaScript鉤子。 2)對於一般樣式,應使用類選擇器,因為它們更靈活和可維護。通過平衡ID和類的使用,可以實現更robust和efficient的CSS架構。

html5'sgoalsin2024focusonrefinement和optimization,notNewFeatures.1)增強performanceandeffipedroptimizedRendering.2)inviveAccessibilitywithRefinedwithRefinedTributesAndEllements.3)explityconcerns,尤其是withercercern.4.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供應,2)語義結構,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,簡化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)