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?
我原本期望两个测试都输出=== b,但只有第一个输出。第二个输出a!= b。谁能解释这种行为?如何在javascript中有效地比较数组?
4 个解决方案
#1
2
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. edit As 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.
您可以编写自己的比较函数,根据您的需要,检查长度是否相同以及元素是否相同(无论是否顺序)。编辑正如其他人所指出的,有几个库包含数组比较函数。如果你发现一个符合你的“平等”定义(并非不可能),并且你不介意考虑将该库纳入你的架构,那可能是个好主意。
#2
25
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
#3
12
Use lodash.
使用lodash。
_.isEqual([1,2],[1,2])
Also works with objects.
也适用于对象。
_.isEqual({a:1}, {a:1})
#4
6
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()才能获得真实副本。
#1
2
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. edit As 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.
您可以编写自己的比较函数,根据您的需要,检查长度是否相同以及元素是否相同(无论是否顺序)。编辑正如其他人所指出的,有几个库包含数组比较函数。如果你发现一个符合你的“平等”定义(并非不可能),并且你不介意考虑将该库纳入你的架构,那可能是个好主意。
#2
25
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
#3
12
Use lodash.
使用lodash。
_.isEqual([1,2],[1,2])
Also works with objects.
也适用于对象。
_.isEqual({a:1}, {a:1})
#4
6
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()才能获得真实副本。