Let say I have 2 arrays
假设我有2个阵列
firstArray = [1, 2, 3, 4, 5];
secondArray = [5, 4, 3, 2, 1];
I want to know if they contain the same elements, while order is not important. I know I can write a function to sort them and then loop through them to check, but is there a pre-built function for this? (not only Vanilla JS, other javascript library is also okay)
我想知道它们是否包含相同的元素,而顺序并不重要。我知道我可以编写一个函数来对它们进行排序,然后循环遍历它们进行检查,但是有没有预先构建的函数呢? (不仅是Vanilla JS,其他javascript库也没关系)
4 个解决方案
#1
4
Using jQuery
You can compare the two arrays using jQuery
:
您可以使用jQuery比较两个数组:
// example arrays:
var firstArray = [ 1, 2, 3, 4, 5 ];
var secondArray = [ 5, 4, 3, 2, 1 ];
// compare arrays:
var isSameSet = function( arr1, arr2 ) {
return $( arr1 ).not( arr2 ).length === 0 && $( arr2 ).not( arr1 ).length === 0;
}
// get comparison result as boolean:
var result = isSameSet( firstArray, secondArray );
Here is a JsFiddle Demo
这是一个JsFiddle演示
See this question helpful answer
看到这个问题有用的答案
#2
1
Well there is an Array.sort()
method in JavaScript, and for comparing the (sorted) arrays, I think it's best to check out this question, as it is has a really good answer.
好吧,JavaScript中有一个Array.sort()方法,为了比较(排序)数组,我认为最好查看这个问题,因为它有一个非常好的答案。
Especially note that comparing arrays as strings (e.g. by JSON.stringify
) is a very bad idea, as values like "2,3"
might break such a check.
特别要注意的是,将数组作为字符串进行比较(例如通过JSON.stringify)是一个非常糟糕的主意,因为像“2,3”这样的值可能会破坏这样的检查。
#3
0
Here's a working implementation using Vanilla JS:
这是使用Vanilla JS的工作实现:
function haveMatchingElements(firstArray, secondArray) {
var stringsInFirstArray = parse(firstArray, 'string'),
stringsInSecondArray = parse(secondArray, 'string'),
numbersInFirstArray = parse(firstArray, 'number'),
numbersInSecondArray = parse(secondArray, 'number'),
stringResults = compare(stringsInFirstArray, stringsInSecondArray),
numberResults = compare(numbersInFirstArray, numbersInSecondArray);
if (stringResults && numberResults) {
return true;
} return false;
function parse(array, type) {
var arr = [];
arr = array.sort().filter(function(index) {
if (typeof index == type)
return index;
});
return arr;
}
function compare(firstArray, secondArray) {
if (firstArray.length !== secondArray.length)
return false;
for (var i = firstArray.length; i--;) {
if (firstArray[i] !== secondArray[i])
return false;
}
return true;
}
}
This parses strings an numbers into different arrays and checks them separately. That will correct the issue of 1
and "1"
matching as true
due to the implicit type conversion caused by the sort
function.
这会将数字解析为不同的数组并单独检查它们。由于sort函数引起的隐式类型转换,这将纠正1和“1”匹配的问题为真。
The implementation is simple:
实现很简单:
var arr1 = ['1', 1];
var arr2 = [1, '1'];
var results = haveMatchingElements(arr1, arr2);
console.log(results); // true
#4
-1
Not in Vanila Javascript but in Angular there is option to match two objects.
不是在Vanila Javascript中,但在Angular中有匹配两个对象的选项。
angular.equals([1,2,3],[1,2,3])
Determines if two objects or two values are equivalent. Supports value types, regular expressions, arrays and objects.
确定两个对象或两个值是否相等。支持值类型,正则表达式,数组和对象。
See if this could help you.
看看这可以帮到你。
alert("Match result of [1,2,3] & [1,2,3] is "+angular.equals([1,2,3],[1,2,3]));
alert("Match result of [1,4,3] & [1,2,3] is "+angular.equals([1,4,3],[1,2,3]));
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
Click on Run Code snippet. If this solves your need please mark it as as Answer :)
单击“运行代码”片段。如果这解决了您的需要请将其标记为答案:)
In case order is not important and array is of number type.
如果顺序不重要且数组是数字类型。
var a1 = [1, 2, 3];
var a2 = [2, 1, 3];
//In case order is not important and array is of number type.
alert(eval(JSON.stringify(a1).replace(/,/g, "+").replace(/\[/g, "").replace(/\]/g, "")) === eval(JSON.stringify(a2).replace(/,/g, "+").replace(/\[/g, "").replace(/\]/g, "")));
#1
4
Using jQuery
You can compare the two arrays using jQuery
:
您可以使用jQuery比较两个数组:
// example arrays:
var firstArray = [ 1, 2, 3, 4, 5 ];
var secondArray = [ 5, 4, 3, 2, 1 ];
// compare arrays:
var isSameSet = function( arr1, arr2 ) {
return $( arr1 ).not( arr2 ).length === 0 && $( arr2 ).not( arr1 ).length === 0;
}
// get comparison result as boolean:
var result = isSameSet( firstArray, secondArray );
Here is a JsFiddle Demo
这是一个JsFiddle演示
See this question helpful answer
看到这个问题有用的答案
#2
1
Well there is an Array.sort()
method in JavaScript, and for comparing the (sorted) arrays, I think it's best to check out this question, as it is has a really good answer.
好吧,JavaScript中有一个Array.sort()方法,为了比较(排序)数组,我认为最好查看这个问题,因为它有一个非常好的答案。
Especially note that comparing arrays as strings (e.g. by JSON.stringify
) is a very bad idea, as values like "2,3"
might break such a check.
特别要注意的是,将数组作为字符串进行比较(例如通过JSON.stringify)是一个非常糟糕的主意,因为像“2,3”这样的值可能会破坏这样的检查。
#3
0
Here's a working implementation using Vanilla JS:
这是使用Vanilla JS的工作实现:
function haveMatchingElements(firstArray, secondArray) {
var stringsInFirstArray = parse(firstArray, 'string'),
stringsInSecondArray = parse(secondArray, 'string'),
numbersInFirstArray = parse(firstArray, 'number'),
numbersInSecondArray = parse(secondArray, 'number'),
stringResults = compare(stringsInFirstArray, stringsInSecondArray),
numberResults = compare(numbersInFirstArray, numbersInSecondArray);
if (stringResults && numberResults) {
return true;
} return false;
function parse(array, type) {
var arr = [];
arr = array.sort().filter(function(index) {
if (typeof index == type)
return index;
});
return arr;
}
function compare(firstArray, secondArray) {
if (firstArray.length !== secondArray.length)
return false;
for (var i = firstArray.length; i--;) {
if (firstArray[i] !== secondArray[i])
return false;
}
return true;
}
}
This parses strings an numbers into different arrays and checks them separately. That will correct the issue of 1
and "1"
matching as true
due to the implicit type conversion caused by the sort
function.
这会将数字解析为不同的数组并单独检查它们。由于sort函数引起的隐式类型转换,这将纠正1和“1”匹配的问题为真。
The implementation is simple:
实现很简单:
var arr1 = ['1', 1];
var arr2 = [1, '1'];
var results = haveMatchingElements(arr1, arr2);
console.log(results); // true
#4
-1
Not in Vanila Javascript but in Angular there is option to match two objects.
不是在Vanila Javascript中,但在Angular中有匹配两个对象的选项。
angular.equals([1,2,3],[1,2,3])
Determines if two objects or two values are equivalent. Supports value types, regular expressions, arrays and objects.
确定两个对象或两个值是否相等。支持值类型,正则表达式,数组和对象。
See if this could help you.
看看这可以帮到你。
alert("Match result of [1,2,3] & [1,2,3] is "+angular.equals([1,2,3],[1,2,3]));
alert("Match result of [1,4,3] & [1,2,3] is "+angular.equals([1,4,3],[1,2,3]));
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
Click on Run Code snippet. If this solves your need please mark it as as Answer :)
单击“运行代码”片段。如果这解决了您的需要请将其标记为答案:)
In case order is not important and array is of number type.
如果顺序不重要且数组是数字类型。
var a1 = [1, 2, 3];
var a2 = [2, 1, 3];
//In case order is not important and array is of number type.
alert(eval(JSON.stringify(a1).replace(/,/g, "+").replace(/\[/g, "").replace(/\]/g, "")) === eval(JSON.stringify(a2).replace(/,/g, "+").replace(/\[/g, "").replace(/\]/g, "")));