Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the intersection of two arrays by es6

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "es6 how to find the intersection of two arrays". In the daily operation, I believe many people have doubts about how to find the intersection of two arrays in es6. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to find the intersection of two arrays by es6". Next, please follow the editor to study!

Implementation method: 1, use the "new Set (array)" statement to convert both arrays to the set collection type; 2, use the "new Set ([... collection 1] .filter (x = > collection 2.has (x))" statement to get the intersection of the two sets, which will return a new collection containing all the intersection elements.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

In es6, you can use the has () method of the set object to match the filter () of the array to find the intersection of two arrays.

Set is a new data structure provided by ES6, which is similar to an array, but does not have duplicate values. With this feature, we can convert the array to the Set type for deduplication, and then use the Array.from method to convert it back to the array.

The Set has () method indicates whether the Set object contains the specified value. Returns true if the specified value exists, otherwise returns 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)

You can see that at this point, the intersection element is returned in a set collection, which can be converted to an array type using the Array.from method

The Array.from method is used to turn two types of objects into real arrays: array-like objects (array-like object) and iterable objects (including ES6's new data structures Set and Map).

Let intersectionSet = Array.from (new Set ([... newA] .filter (x = > newB.has (x); console.log (intersectionSet)

Extended knowledge: Union / difference set

Let a = new Set; let b = new Set ([3meme 5,2]); / / Union let unionSet = new Set ([... a,... b]); / / [1Yue2jinsheng5] / / ab difference set let differenceABSet = new Set ([... a] .filter (x = >! b.has (x) At this point, the study of "how to find the intersection of two arrays by es6" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report