/    Sign up×
Community /Pin to ProfileBookmark

Testing array equality

If I define two arrays identically and test for their equality I get a “false”.
That is,

[CODE]
var ara=[];
ara[0]=[1,2,3];
ara[1]=[1,2,3];
var x=0;
if (ara[0] == ara[1]) {x = -1}
alert(x);
[/CODE]

produces an alert of 0.

So I thought perhaps == could not be used to compare arrays.

But the following

[CODE]
var ara=[];
ara[0]=[1,2,3];
ara[1]=[1,2,3];
ara[0] = ara[1];
var x=0;
if (ara[0] == ara[1]) {x = -1}
alert(x);
[/CODE]

produces an alert of -1.

Can someone tell me why the first comparison fails?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangAug 07.2010 — ara[0].sort().toString() == ara[1].sort().toString()
Copy linkTweet thisAlerts:
@jackacohenauthorAug 07.2010 — In my actual code I had already sorted the arrays to be compared.

So I'll assume that your .toString() is the key which will make the comparison work (for practical purposes), and say [B]thank you, Fang[/B].

But of course then the comparison is of two strings. Can you tell me why the comparison of arrays fails, given that == does work on arrays?

That is, [U]what[/U] is being compared in the first case above that is different from what is being compared in the second case, and which causes js to miss the equality?
Copy linkTweet thisAlerts:
@Declan1991Aug 07.2010 — Two objects. Just because two arrays have the same elements does not make them the same object. Compare.var a = [1,2,3];
var b = a;
alert(a == b);
You have to decide what you mean by equal arrays (i.e. equal elements) and implement that yourself. What about nested arrays? Is [1,[1,2]] equal to [1,[1,2]], you've got to decide.
Copy linkTweet thisAlerts:
@jackacohenauthorAug 07.2010 — First, thank you to all the experts who so wilingly give their time to answer these questions.

I don't want to waste anyone's time, but if I recall correctly, when I was a [U]complete[/U] newbie (to js) I learned rather a lot from asking follow-up questions here about "passing by value" versus "passing by reference", so in the hope of gaining a deeper understanding of what's happening "backstage" let me ask the following:

You have to decide what you mean by equal arrays [/QUOTE]
What I want to mean for arrays a and b being equal is the intuitive meaning:

[B]Def:[/B] a=b [U]iff[/U] a.length=b.length and for (i=0; i<a.length; i++) (a[i] = b[i])



noting that if, for some i, a[i].length>1 the definition must be applied recursively.



The question I still have is WHAT js is comparing when it tests (a == b) that results in "false".



It can't just be "object == object" (can it?)





And I don't understand the question

Is [1,[1,2]] equal to [1,[1,2]]?[/QUOTE]

By my definition above (and intuitively) the answer is "yes".



But the question led me to note that the .toString() solution results in a false (according to the def above) positive for

[CODE]a = [ 1, [2,3] ]
b = [ [1,2], 3 ]
if (a.toString()==b.toString() ) ...[/CODE]

I hadn't thought of this earlier because in the context in which I'm comparing arrays none of the elements of the arrays are themselves arrays.

In the meantime, this seems to eliminate the problem

[CODE]function xEQy(x,y)
{
var binVar=false;
if (x.length == y.length) {binVar = true}
if (!binVar) {return binVar};

var binVar=true;
for (i=0; i<x.length; i++)
{
if (x[i].length>0) {binVar = xEQy(x[i],y[i])}
else { if (x[i] != y[i]) {binVar=false} };
}
return binVar;
}

var a=[1,[2,3]]
var b=[1,[2,3]];

alert( xEQy(a,b) )[/CODE]


But I'm always leary of functions calling themselves, lest I run out of stack space. But for now this will do - unless someone wants to tell me why it should not be used.
Copy linkTweet thisAlerts:
@Declan1991Aug 07.2010 — Objects aren't necessarily equal if they appear intuitively to be so, in fact, they never are, unless they refer to exactly the same thing (not just the same elements).

As far as I know, the function you've shown or a variation of it is the only way to test for equality with arrays. But you probably should sort the two aswell, unless you want [1, 2] and [2, 1] to be different. Also, maybe typeof would be a better test for x[i], because other objects (strings for example) can have length.



EDIT: I knew I remembered seeing an example here before. [url=http://www.webdeveloper.com/forum/showthread.php?p=396680#post396680]Array comparator[/url] by Big Moosie. Your one is probably marginally more efficient, but his is probably more comprehensive.
×

Success!

Help @jackacohen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...