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


Groups > comp.lang.javascript > #24440

Re: Difference between two arrays

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Difference between two arrays
Date Tue, 27 May 2014 17:21:21 +0200
Organization PointedEars Software (PES)
Lines 84
Message-ID <3407999.BczK8MDucV@PointedEars.de> (permalink)
References <z_ydnZKbtslwbR7OnZ2dnUVZ_rGdnZ2d@westnet.com.au>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1401204084 16440 eJwFwQkBACAIA8BKgAw1jjzrH8E7rNCo7YFwEJxkj7vsrW+gyXs97jFhUWx1og+854mWKe0DKS8RXA== (27 May 2014 15:21:24 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Tue, 27 May 2014 15:21:24 +0000 (UTC)
User-Agent KNode/4.12.4
X-User-ID eJwNysERACAIA8GWFCVCORKg/xL0cZ+d04UJng3F1tZuruh08YzKuqJ7FMDoMhgHXP5ItxuJD6tOmPEnNTF5H4QaFnA=
Cancel-Lock sha1:5V6cTHfRBaZHNtpBWaqC7bx+dYk=
X-NNTP-Posting-Host eJwFwQkBACAIBLBKIn8cQK5/BDdlIxsXUxOFYrfZqvAiShwQgG4u9UnPc9m5ZWKz51U35AM4dhIr
Xref csiph.com comp.lang.javascript:24440

Show key headers only | View raw


Andrew Poulos wrote:

> If I have two "simple" arrays and I need to create a third array of
> elements that are only in one of the arrays. I found this
> 
> Array.prototype.difference = function (a) {
>    return this.filter(function (i) {

This returns the elements of the calling array (“this”)…

>      return !(a.indexOf(i) > -1);

… that are *not* in the array referred to by “a” (“a”).  “i” is the value of 
the respective element of “this” in each call of the function.  (I would 
label it “e” for “element” instead, to avoid confusion with the index.)

If “i” is found in “a”, a.indexOf(i) returns the index of “i” in “a”.  The 
index of standard arrays is always greater than -1 as it starts with 0.

If “i” is not found in “a”, -1 is returned.  Boolean-inverting the 
expression “a.indexOf(i) > -1” thus evaluates to “true” if “a.indexOf(i) > 
-1” evaluates to “false”, that is, if “i” was not found in “a”.  Likewise, 
it evaluates to “false” if “a.indexOf(i) > -1” evaluates to “true”, that is, 
if “i” was found in “a”. 

>    });
> };
> 
> which I don't fully understand but the issue with it that I have is that
> I need to run it on both arrays to get all the differences. For example
> 
> var arrX = [1, 2, 4, 6, 8],
>      arrY = [4, 8, 9];
> 
> var arrRes1 = arrX.difference(arrY)); // 1,2,6
> var arrRes2 = arrY.difference(arrX)); // 9
> 
> var arrRes = arrRes1.concat(arrRes2); // 1,2,6,9

There may be elements in array #1 that are not in array #2; for example, 
array #1 contains 1, 2 and 6 here which are not in array #2.  Those are 
obtained if you run “difference()” on array #1 and pass array #2.

There may also be elements in array #2 that are not in array #1; for 
example, array #2 contains 9 here which is not in array #1.  Those are 
obtained if you run the method on array #2 and pass array #1.

Logic (set theory) dictates that the elements of the sets A an B that are 
either in A or in B (which *you* call the “differences” between A and B) can 
be obtained by the union {A ∖ B} ∪ {B ∖ A} [{A \ B} U {B \ A} from here]:

                 A  \ B

             ,-'''''..'''''.
            :/1////:4 :     :
          A :///2//: 8:  9  : B
            ://///6:  :     :
             '._____''____.'


                 B  \ A

             ,-'''''..'''''.
            : 1    :4 ://///:
          A :   2  : 8://9//: B
            :     6:  ://///:
             '._____''____.'


            {A \ B} U {B \ A}

             ,-'''''..'''''.
            :/1////:4 ://///:
          A :///2//: 8://9//: B
            ://///6:  ://///:
             '._____''____.'

HTH

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


Thread

Difference between two arrays Andrew Poulos <ap_prog@hotmail.com> - 2014-05-27 11:55 +1000
  Re: Difference between two arrays Denis McMahon <denismfmcmahon@gmail.com> - 2014-05-27 05:58 +0000
  Re: Difference between two arrays John C <rescattered@gmail.com> - 2014-05-27 03:34 -0700
    Re: Difference between two arrays Spamless <Spamless@Nil.nil> - 2014-05-31 06:35 -0500
      Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-31 14:57 +0200
        Re: Difference between two arrays Spamless <Spamless@Nil.nil> - 2014-06-01 03:43 -0500
          Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-01 14:01 +0200
            Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-01 16:53 +0200
  Re: Difference between two arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-27 13:41 +0100
    Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 17:41 +0200
    Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 17:49 +0200
      Re: Difference between two arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-27 17:34 +0100
        Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 19:33 +0200
          Re: Difference between two arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-27 20:03 +0100
    Re: Difference between two arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-27 17:16 +0100
      Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 19:29 +0200
  Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 17:21 +0200
  Re: Difference between two arrays Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-28 18:22 +0100
    Re: Difference between two arrays Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-30 22:23 +0100
    Re: Difference between two arrays "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-05-31 14:59 -0700
      Re: Difference between two arrays "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-06-01 00:50 +0200
        Re: Difference between two arrays "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-05-31 16:17 -0700
          Re: Difference between two arrays "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-06-01 10:49 +0200
            Re: Difference between two arrays "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-01 13:37 -0700
      Re: Difference between two arrays Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-06-01 18:56 +0100

csiph-web