How to compare the values ​​of two arrays in es6

青灯夜游
Release: 2022-03-09 19:28:49
Original
2701 people have browsed it

Every() and some() can be used for comparison in es6, the syntax "s1.length===s2.length&&s1.every(a=>s2.some(b=>a=== b))&&s2.every(_b=>s1.some(_a=>_a===_b))”.

How to compare the values ​​of two arrays in es6

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

es6 compares whether the values ​​​​of two arrays are equal (regardless of the order of the arrays)

Implementation code: (Convert the two arrays Replace it with your own array)

let  listA = val;//当前选中
let  listB = this.plainOptions;//数据
let  result = listA.length === listB.length && listA.every(a => listB.some(b => a === b)) && listB.every(_b => listA.some(_a => _a === _b));
Copy after login

Usage method api:

1、every

Every() method is used to detect whether all elements of the array meet the specified conditions (provided through the function).

The every() method uses the specified function to detect all elements in the array:

If one element in the array is detected to be unsatisfactory, the entire expression returns false, and the remaining elements will not Test again.

Returns true if all elements meet the condition.

Note: every() will not detect empty arrays, and every() will not change the original array.

Grammar:

array.every(function(currentValue,index,arr), thisValue)
Copy after login

Parameter description:

How to compare the values ​​of two arrays in es6

##2, some:

some () method is used to detect whether the elements in the array meet the specified conditions (provided by the function).

The some() method will execute each element of the array in sequence:

If one element meets the condition, the expression returns true, and the remaining elements will not be tested again.

If there is no element that meets the condition, return false.

Note: some() will not detect empty arrays, and some() will not change the original array.

Grammar:

array.some(function(currentValue,index,arr),thisValue)
Copy after login
Parameter description:

How to compare the values ​​of two arrays in es6

[Related recommendations:

javascript video tutorial, web front end

The above is the detailed content of How to compare the values ​​of 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