Javascript 如何在javascript中检查数组是否相等?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12770373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if arrays are equal in javascript?
提问by Shawn
Have a look at this code sample or go to the jsfiddle
查看此代码示例或转到 jsfiddle
function printRelation(a, b, out) {
var text;
if (a === b) {
text = "a === b";
} else if (a == b) {
text = "a == b";
} else {
text = "a != b";
}
$('#' + out).text(text);
}
var a = [0, 0, 2], b = a;
printRelation(a, b, 'out1');
a = [0, 0, 2];
b = [0, 0, 2];
printRelation(a, b, 'out2');
I would have expected both tests to output a === b
, but only the first one does. The second one outputs a != b
. Can anyone explain this behaviour? How can I efficiently compare arrays in javascript?
我原以为两个测试都会输出a === b
,但只有第一个会输出。第二个输出a != b
. 谁能解释这种行为?如何有效地比较javascript中的数组?
采纳答案by Pointy
JavaScript array comparisons like you've written are just simple object reference comparisons. They're not "deep" comparisons element by element.
像您编写的 JavaScript 数组比较只是简单的对象引用比较。它们不是逐个元素的“深度”比较。
You can write your own comparison function, something to check that the lengths are the same and the the elements are the same, either in order or not, as fits your needs. editAs others point out, there are several libraries that include array comparison functions. If you find one that meets your definition of "equality" (not unlikely), and you don't mind considering incorporating that library into your architecture, that might be a good idea.
您可以编写自己的比较函数,以检查长度是否相同以及元素是否相同,无论是否按顺序,都适合您的需要。编辑正如其他人指出的那样,有几个库包含数组比较函数。如果您发现一个符合您对“平等”的定义(并非不可能),并且您不介意考虑将该库合并到您的架构中,那么这可能是一个好主意。
回答by RKumsher
You can use the Underscore.js library's isEqual method.
您可以使用 Underscore.js 库的 isEqual 方法。
http://underscorejs.org/#isEqual
http://underscorejs.org/#isEqual
Performs an optimized deep comparison between the two objects, to determine if they should be considered equal.
在两个对象之间执行优化的深度比较,以确定它们是否应该被视为相等。
var moe = {name : 'moe', luckyNumbers : [13, 27, 34]};
var clone = {name : 'moe', luckyNumbers : [13, 27, 34]};
moe == clone;
=> false
_.isEqual(moe, clone);
=> true
UPDATE
更新
Lodash is inspired by underscore, but nowadays is superior solution
Lodash 受到下划线的启发,但现在是更好的解决方案
https://lodash.com/docs/#isEqual
https://lodash.com/docs/#isEqual
Performs an optimized deep comparison between the two objects, to determine if they should be considered equal.
在两个对象之间执行优化的深度比较,以确定它们是否应该被视为相等。
var object = { 'a': 1 };
var other = { 'a': 1 };
_.isEqual(object, other);
// => true
object === other;
// => false
回答by Stefan Kendall
回答by David Hellsing
Arrays are Objects in javascript. And comparing objects can't be done using ==
like you would normally use when comparing strings or numbers.
数组是 JavaScript 中的对象。并且不能==
像在比较字符串或数字时通常使用的那样使用比较对象。
There are many other ways to compare arrays, my favourite is using JSON:
还有很多其他方法可以比较数组,我最喜欢使用 JSON:
if ( JSON.stringify(a) == JSON.stringify(b) ) {
// equal
Note that assigning a pre-existing object to a variable like you do when you write b = a
is just copying a reference. They both point to the same array.
请注意,将预先存在的对象分配给变量就像您在编写时所做的那样b = a
只是复制引用。它们都指向同一个数组。
You would have to do b = a.slice()
to get a real copy.
你必须做b = a.slice()
才能获得真正的副本。