Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #10607

Re: Comparing arraies

Message-ID <1934880.aqSPkdTlGX@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-01-26 19:50 +0100
Subject Re: Comparing arraies
Newsgroups comp.lang.javascript
References <8dc6b834-cd20-47a5-a085-f4d5e4402c38@c13g2000yqk.googlegroups.com> <4f218d62$0$6578$9b4e6d93@newsspool3.arcor-online.net>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Martin Honnen wrote:

> Archos wrote:
>> How to know if 2 arraies are equal? I was expecting that it could be
>> checked so:
>>>>> var array1 = [1,2,3,4], array2 = [1,2,3,4];
>>>>> if (array1 === array2) { console.log("array1 and array2 are equal"); }
>>
>> Since the comparing of strings or integers works:
>>
>> var a = "2", b = "2";
>> if (a === b) { console.log("a == b"); }
> 
> No, for objects (and arrays are objects) the identity is checked, no
> comparison of properties or elements is done.
> So with e.g.
> 
> var a1, a2;
> 
> a1 = a2 = [1, 2, 3, 4];
> 
> a1 == a2
> 
> you get true as the two variables hold references to the same array
> object.

Correct.  That example might be confusing, though.

> If you want to compare the elements you will need to define and
> implement your own comparison function I think

Not necessarily.  One way without a custom comparison function that works 
for *simple* cases like this is to compare the string representations of the 
Array instances, which are (without overwriting the toString() method) the 
comma-separated string representations of the corresponding array's 
elements:

  /*  "1,2,3,4" == "1,2,3,4"  */
  a1.toString() == a2.toString()

or

  String(a1) == String(a2)

or

  ("" + a1) == ("" + a2)

That is, those two arrays are considered equal if the string representations 
of their elements are the same and occur in the same order (if we assume 
that both instances use the same toString() method).

The caveat is that the string representations of elements can be the same, 
although the elements are different.  Consider this:

  var o = {foo: 'bar'};
  var o2 = {bar: 'baz'};

  /* "[object Object]" == "[object Object]" */
     [o].toString()    == [o2].toString()

Instead of the `==' operator, the `===' operator may be used as well, but it 
is neither supposed to add efficiency nor would it add expressive power 
(both arguments are of the same type), while it certainly reduces the 
backwards-compatibility of the entire code.


PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Comparing arraies Archos <raul.san@sent.com> - 2012-01-26 07:23 -0800
  Re: Comparing arraies Martin Honnen <mahotrash@yahoo.de> - 2012-01-26 18:29 +0100
    Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-26 19:50 +0100
      Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-26 19:54 +0100
        Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-01-27 16:49 +0000
          Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-27 18:41 +0100
            Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-01-28 11:28 +0000
              Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-28 21:17 +0100
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-01-29 14:55 +0000
                Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-29 17:13 +0100
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-01-30 15:27 +0000
                Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-30 19:03 +0100
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-01-31 15:07 +0000
                Re: Comparing arraies Dr J R Stockton <reply1205@merlyn.demon.co.uk> - 2012-01-31 18:51 +0000
                Re: Comparing arraies "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-01-30 12:01 -0800
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-01-31 11:06 +0000
                Re: Comparing arraies "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-01-31 06:08 -0800
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-01-31 16:08 +0000
                Re: Comparing arraies "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-01-31 18:32 -0800
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-02-01 17:02 +0000
                Re: Comparing arraies "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-02-01 10:07 -0800
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-02-02 15:10 +0000
                Re: Comparing arraies "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-02-02 11:16 -0800
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-02-07 15:14 +0000
                Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-01 21:45 +0100
                Re: Comparing arraies "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-02-01 13:07 -0800
                Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-01 23:44 +0100
                Re: Comparing arraies "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-02-01 18:22 -0800
                Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-02 23:09 +0100
                Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-03 01:47 +0100
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-02-04 10:34 +0000
                Re: Comparing arraies Dr J R Stockton <reply1205@merlyn.demon.co.uk> - 2012-02-05 16:47 +0000
                Re: Comparing arraies John G Harris <john@nospam.demon.co.uk> - 2012-02-06 15:21 +0000
                Re: Comparing arraies "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-02-04 23:37 -0800
    Re: Comparing arraies Denis McMahon <denismfmcmahon@gmail.com> - 2012-01-26 23:08 +0000
      Re: Comparing arraies Denis McMahon <denismfmcmahon@gmail.com> - 2012-01-27 01:42 +0000
        Re: Comparing arraies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-27 11:41 +0100
      Re: Comparing arraies Gene Wirchenko <genew@ocis.net> - 2012-01-26 16:24 -0800
        Re: Comparing arraies "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-01-27 08:50 +0000
      Re: Comparing arraies Dr J R Stockton <reply1204@merlyn.demon.co.uk> - 2012-01-27 21:07 +0000
        Re: Comparing arraies "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-01-28 13:45 +0000
  Re: Comparing arraies "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-01-26 16:21 -0800
    Re: Comparing arraies Martin Honnen <mahotrash@yahoo.de> - 2012-01-27 16:00 +0100

csiph-web