Set 2.has(x))))" to obtain the difference set; 3. Determine the difference Whether the set array is an empty array, if so, the two arrays are the same, otherwise they are not equal."/> Set 2.has(x))))" to obtain the difference set; 3. Determine the difference Whether the set array is an empty array, if so, the two arrays are the same, otherwise they are not equal.">

Home  >  Article  >  Web Front-end  >  How to determine whether two arrays are equal in es6

How to determine whether two arrays are equal in es6

青灯夜游
青灯夜游Original
2022-10-18 19:54:552914browse

Steps: 1. Use the length attribute to get the length of the two arrays, and determine whether the two lengths are equal. The syntax is "array 1. length == array 2. length"; 2. If the lengths are equal, use " new Set (array)" converts both arrays into set types and obtains them with "Array.from(new Set([...set 1].filter(x=> set 2.has(x))))" Difference set; 3. Determine whether the difference set array is an empty array. If so, the two arrays are the same, otherwise they are not equal.

How to determine whether two arrays are equal in es6

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

To determine whether two arrays are equal, you can change the idea to determine whether the lengths of the two arrays are equal and whether the difference set (containing different elements) is empty.

Implementation idea:

  • Check whether the lengths of the two arrays are equal

  • If the lengths are equal, Then determine whether the difference set of the two arrays is empty

    • If the difference set is empty, the two arrays are equal (because there are no different elements)

    • If the difference set is not empty, the two arrays are not equal (because there are different elements)

Explanation: If the array has duplicate values, The array lengths are different, but there are no different elements (the difference set is empty)

var a=[1, 2, 3];
var b=[1,2,3,1,3];

How to determine whether two arrays are equal in es6

But such two arrays cannot be equal, so you need to first determine whether the array length is equal.

Implementation steps:

Step 1. Use the length attribute to obtain the lengths of the two arrays respectively, and determine whether the two lengths are equal.

Each array has a length attribute, which can be used to return the maximum length of the array, that is, its value is equal to the maximum subscript value plus 1.

var a=[1, 2, 3];
var b=[1, 2, 3];
console.log(a);
console.log(b);

if(a.length==b.length){
	console.log("两数组的长度相等");
}else{
	console.log("两数组的长度不相等");
}

How to determine whether two arrays are equal in es6

Step 2: If the two lengths are equal, use the has(), filter(), and from() methods to obtain the difference between the two arrays

has() is a method of the set object, so if you want to use the has() method, you need to convert the array to a set collection type first.

newA = new Set(a);
newB = new Set(b);

The has() method of the set object cooperates with the filter() of the array to find the intersection of the two arrays, but the intersection elements will be included in a set collection and returned, making it difficult to perform a null comparison; therefore, You need to use the Array.from method to convert the collection into an array type.

let differenceABSet = Array.from(new Set([...newA].filter(x => !newB.has(x))));

How to determine whether two arrays are equal in es6

Description:

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

Step 3: Determine whether the difference set array is an empty array

  • If the difference set array is an empty array, then The two arrays are the same

  • If the difference array is not an empty array, the two arrays are not the same

if(differenceABSet.length==0){
	console.log("两数组相等");
}else{
	console.log("两数组不相等");
}

How to determine whether two arrays are equal in es6

Complete implementation code (encapsulated as a function)

function f(a, b) {
	newA = new Set(a);
	newB = new Set(b);
	if (a.length == b.length) {
		let differenceABSet = Array.from(new Set([...newA].filter(x => !newB.has(x))));
		console.log("两数组差集:");
		console.log(differenceABSet);
		if (differenceABSet.length == 0) {
			console.log("两数组相等");
		} else {
			console.log("两数组不相等");
		}

	} else {
		console.log("两数组不相等");
	}
}

Example 1: Check whether the following two functions are equal

var a = [1, 2, 3];
var b = [1, 2, 3,3];
console.log(a);
console.log(b);
f(a, b);

How to determine whether two arrays are equal in es6

Example 2: Check whether the following two functions are equal

var a = [1, 2, 3];
var b = [1, 2, 4];
console.log(a);
console.log(b);
f(a, b);

How to determine whether two arrays are equal in es6

Explanation: The elements in the difference set are from the compared array (the first arraya).

【Related recommendations: javascript video tutorial, programming video

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

Statement:
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
Previous article:Does nodejs support es6?Next article:Does nodejs support es6?