Have a look at this code sample or go to the 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?
-
Possible duplicate of stackoverflow.com/questions/3115982/…JConstantine– JConstantine2012年10月07日 16:01:33 +00:00Commented Oct 7, 2012 at 16:01
-
I guess the === operator compares arrays by reference, which would explain the observed behaviour. EDIT: Answered by Pointy in the meantime.Thomas– Thomas2012年10月07日 16:01:48 +00:00Commented Oct 7, 2012 at 16:01
4 Answers 4
You can use the Underscore.js library's isEqual method.
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
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
1 Comment
_.isEqual([1, 2, 3], [3, 2, 1]) => false
1 Comment
Arrays are Objects in javascript. And comparing objects can’t be done using ==
like you would normally use when comparing strings or numbers.
There are many other ways to compare arrays, my favourite is using 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.
You would have to do b = a.slice()
to get a real copy.
6 Comments
JSON.stringify
'ing the arrays has other limitations as well since JSON supports only a subset of the value types supported in JavaScript. I personally think it's overkill to create JSON strings, especially considering its serious limitations.for ... in
loops. Of course it can generally be relied upon, but it's a little worrisome to do so from a formal standpoint.JavaScript array comparisons like you've written are just simple object reference comparisons. They're not "deep" comparisons element by element.
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.
10 Comments
17
in the other. The point is that the meaning of "equal" is not necessarily always the same. If a library is already in use that contains some generic array equality test, then of course it would be foolish not to use it.