2

Assume that I have this array:

a = [
    [2823832908, 10071920], 
    [5384625228, 10924221], 
    [8488934028, 313411415], 
    [2823828588, 10071580], 
    [5224682868, 14919881], 
    [8155986228, 560217208], 
    [3458951628, 10071570], 
    [6382592388, 25064430], 
    [5021452668, 10924221], 
    [8827673748, 59397160], 
    [8647215588, 26343621]
]

and this array:

b = [
    [8488934028, 313411415], 
    [8647215588, 26343621]
]

How can I get a new array that contains the values in array a that are not in array b, or how can I remove the values in array b from array a?

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
dr_sd2001
  • 25
  • 7

4 Answers4

2
  1. Create an empty result array.
  2. Iterate over a, select its current element i.e. [2823832908, 10071920]

  3. Compare current element for equality against each element of b , to compare equality you can use JSON.stringify or .join to create string representation of arrays.

  4. If the current element does not match any element of b, appent it to result array.

  5. Repeat.

  a = [
                [2823832908, 10071920], 
                [5384625228, 10924221], 
                [8488934028, 313411415], 
                [2823828588, 10071580], 
                [5224682868, 14919881], 
                [8155986228, 560217208], 
                [3458951628, 10071570], 
                [6382592388, 25064430], 
                [5021452668, 10924221], 
                [8827673748, 59397160], 
                [8647215588, 26343621]
];
    b = [
        [8488934028, 313411415], 
        [8647215588, 26343621]
    ]; 


    var result = [];
    a.forEach(
        function(elem,idx,arr)
        {
            var sig = JSON.stringify(elem);
            var match = false;
            for(var i=0;i<b.length;i++)
            {
                if(sig == JSON.stringify(b[i]))
                   {
                     match = true;
                     break;
                   }
            }
            if(match === false)
                {
                    result.push(elem);
                }
        }
        );

     console.log(result);  

demo : http://jsfiddle.net/Ag39M/4/

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • 1
    This is a good answer, but it's worth noting that `JSON.stringify` is not a great way to compare arrays. [See the comparison in our methods' performance for why](http://jsperf.com/filtering-arrays). – lonesomeday May 29 '14 at 14:04
  • @lonesomeday Thanks for the performance analysis. Yes,the OP can use more optimized methods to compare the arrays. I used JSON.stringify assuming smaller inputs and its simplicity. – DhruvPathak May 29 '14 at 14:10
1

This is a bit tricky, principally because two arrays are not equal in Javascript, even if they have exactly the same keys.

You have to compare them manually, for example with this function by Tim Down.

You will then have to loop through the values in first array and compare them to every value in the second array.

The code might look like this:

var filtered = [];

// loop through every element in a
a.forEach(function(elA) {
    // loop through every element in b
    // if the elA is identical to any elB, some will immediately return true
    // if elA is not identical to any elB, it will return false
    var found = a.some(function(elB) {
        return arraysIdentical(elA, elB);
    });

    // if we didn't find any equal ones, we'll add elA to the filtered array
    if (!found) {
        filtered.push(elA);
    }
});

Note that this relies upon Array#forEach and Array#some. If you need to support older browsers, you'll need to shim them, with the polyfill code in those links.

Community
  • 1
  • 1
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
1

Here's an implementation using undescore.js.

function diff(a1, a2) {
  return _.filter(a1, function(e1) {
    return !_.some(a2, function(e2) {
      return _.isEqual(e1, e2);
    });
  });
}

Example:

var a = [ [ 1, 2 ], [ 3, 4 ], [ 5, 6, 7 ], [ 8 ], [ 9 ] ];
var b = [ [ 1, 2 ], [ 8 ], [ 9 ] ];

console.log(diff(a, b));
// [ [ 3, 4 ], [ 5, 6, 7 ] ]
Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
0
function arra_diff(a, b)
{
  var arr=[], diff=[];
  for(var i=0;i<a.length;i++)
    arr[a[i]]=true;
  for(var i=0;i<b.length;i++)
    if(arr[b[i]]) delete arr[b[i]];
    else arr[b[i]]=true;
  for(var k in arr)
    diff.push(k);
  return diff;
}

or

function arra_diff(d, c) { /* d=a,c=b */
    for (var b = [], e = [], a = 0; a < d.length; a++) {
        b[d[a]] = !0;
    }
    for (a = 0; a < c.length; a++) {
        b[c[a]] ? delete b[c[a]] : b[c[a]] = !0;
    }
    for (var f in b) {
        e.push(f);
    }
    return e;
};
  • I've voted this down not because it's wrong but because it's unreadable and uncommented and is therefore not a good StackOverflow answer. – lonesomeday May 29 '14 at 13:58