0

I'm sure this is very easy (I'm a beginner!), but I couldn't find a solution.

I would like to import two arrays from .csv-files, then compare the two and return the values which DO appear on List1 and are NOT on List2. So List1 minus List2 = my result.

List1 could look like: 100,200,300,400 and List2 100,200. I would then like to return 300,400.

(If List 2 should happen to have some values which are not in List 1, I'd like it to run anyway and not give an error, but I figure it wouldn't?)

Thanks in advance for any tips/pointers or code, should you be so kind :-)

  • 2
    possible duplicate of [What is the fastest or most elegant way to compute a set difference using Javascript arrays?](http://stackoverflow.com/questions/1723168/what-is-the-fastest-or-most-elegant-way-to-compute-a-set-difference-using-javasc) – dee-see Sep 16 '14 at 21:07
  • do you want speed or simplicity? – dandavis Sep 16 '14 at 21:14
  • Definitely simplicity! Each array will have less than 50 values. – captchacha Sep 17 '14 at 23:02

3 Answers3

0

Let's say you have 2 strings with , separated values like

var a= "100, 200, 400";
var b = "100, 200, 300";

You could concatenate the values like, using String.split convert them into an array:

var items = (a +","+ b).split(',');

And then could do this in several ways one is using Array.prototype.reduce like

items.reduce(function(a,c){  var count = a.filter(function(item){ return item ===c; }).length; if(count<1 ) a.push(c); return a;},[]);
Dalorzo
  • 19,834
  • 7
  • 55
  • 102
0

This appears to work:

var array1 = [100,200,300,400];
var array2 = [200,100,400];
function array_diff(arr1,arr2)
{
    var x = Math.max(arr1.length,arr2.length);
    for(var i = 0; i < x; i++)
    {
        if(typeof arr1[i] !== 'undefined' && arr2.indexOf(arr1[i]) !== -1)
            arr1.splice(i,1);
        if(typeof arr2[i] !== 'undefined' && arr1.indexOf(arr2[i]) !== -1)
            arr1.splice(arr1.indexOf(arr2[i]),1);
    }
    return arr1;
}
console.log(array_diff(array1,array2));

I tried to avoid using Array.prototype.filter and Array.prototype.reduce because they are not compatible with older browsers. As far as I know, Array.prototype.splice has better support.

If you're looking for speed gains, I could be wrong but, I think sorting the arrays beforehand would speed up indexOf.

http://jsfiddle.net/0tcyam2f/1/

Alex W
  • 37,233
  • 13
  • 109
  • 109
0

Thanks for your answers!

I'm afraid this code is a little over my understanding still, so I just tested the various options, and the one that seems to give the desired result without any error messages is the one from What is the fastest or most elegant way to compute a set difference using Javascript arrays? , as rightly pointed out by Vache, that is:

A = [100,200,300];
B = [100,200,500];

diff_set = {
    ar : {},
    diff : Array(),
    remove_set : function(a) { ar = a; return this; },
    remove: function (el) {
        if(ar.indexOf(el)<0) this.diff.push(el);
    }
};

A.forEach(diff_set.remove_set(B).remove,diff_set);
C = diff_set.diff;

originally posted by Xavi Ivars

Thanks for providing answers!! Much appreciated :-)

Community
  • 1
  • 1