Home > Web Front-end > Front-end Q&A > How to find different items in two arrays in es6

How to find different items in two arrays in es6

青灯夜游
Release: 2022-11-01 18:07:38
Original
2772 people have browsed it

Steps: 1. Convert the two arrays to set types respectively, with the syntax "newA=new Set(a);newB=new Set(b);"; 2. Use has() and filter() To find the difference set, the syntax is "new Set([...newA].filter(x =>!newB.has(x)))". The difference set elements will be included in a set collection and returned; 3. Use Array .from converts the collection into an array type, the syntax is "Array.from (collection)".

How to find different items in two arrays in es6

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

Finding the different items in two arrays is to obtain the difference between the two arrays.

In es6, you can use the has() method of the set object to find the difference between two arrays.

Implementation steps:

Step 1. Convert the two arrays to set types

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

How to find different items in two arrays in es6

Step 2: Find the difference set

Use the has() method of the set object in conjunction with the filter() of the array to find the difference between the two arrays set.

  • The Set has() method indicates whether the Set object contains the specified value; if the specified value exists, it returns true, otherwise it returns false.

  • The filter() method is used to filter the array and return elements that meet the conditions (true).

Implementation code

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);
Copy after login

How to find different items in two arrays in es6

It can be seen that at this time, the difference set elements are included in a set set and returned, we can Convert it to array type.

Step 3: Use the Array.from method to convert the collection into an array type

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);
Copy after login

How to find different items in two arrays in es6

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

Expand knowledge: finding union/intersection

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)));
Copy after login

[Related recommendations: javascript video tutorial, web front-end]

The above is the detailed content of How to find different items in two arrays in es6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template