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))”.
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));
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)
Parameter description:
##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)
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!