0

I have these 2 arrays

var array1 = 
      [ { name: 'placeone', leagueID: '8368223' },
      { name: 'placetwo', leagueID: '6164631' },
      { name: 'placethree', leagueID: '4564836' },
      { name: 'placefour', leagueID: '9722578' },
      { name: 'placefive', leagueID: '9722578' }];
    var array2 = 
      [{name: 'placeone', leagueID: '8368223' },
      {name: 'placetwo', leagueID: '6164631' },
      {name: 'placethree', leagueID: '4564836' },
      {name: 'placefour', leagueID: '9722578' },
      {name: 'placesix', leagueID: '9722578' }];

I would like to remove all duplicated results, both of them leaving only:

  [{ name: 'placefive', leagueID: '9722578' },
  { _id: 55b7f4825d3255b043e3dfe8, name: 'placesix', leagueID: '9722578', __v: 0 }]

I have the following function, but I don't need to reuse any of this if you I am barking up the wrong tree :)

var unquie = function (array1, array2, name) {
    var myArr = array1.concat(array2);
    var newArr = myArr;

    for(var h = 0; h < myArr.length; h++) {
        var curItem = myArr[h][name];
        var foundCount = 0;
        // search array for item
        for(var i = 0; i < myArr.length; i++) {
            if (myArr[i][name] === myArr[h][name])
                foundCount++;
        }
        if(foundCount > 1) {
            // remove repeated item from new array
            for(var j = 0; j < newArr.length; j++) {
                if(newArr[j][name] === curItem) {
                    newArr.splice(j, 1);
                    j = j - 1;
                }
            }
        }
    }

    return newArr;
};
unquie(array1, array2, 'name');
//Random incorrect results :(

var array1 = 
      [ { name: 'placeone', leagueID: '8368223' },
      { name: 'placetwo', leagueID: '6164631' },
      { name: 'placethree', leagueID: '4564836' },
      { name: 'placefour', leagueID: '9722578' },
      { name: 'placefive', leagueID: '9722578' }];

    var array2 = 
      [{name: 'placeone', leagueID: '8368223' },
      {name: 'placetwo', leagueID: '6164631' },
      {name: 'placethree', leagueID: '4564836' },
      {name: 'placefour', leagueID: '9722578' },
      {name: 'placesix', leagueID: '9722578' }]


console.info('Original Arrays');
console.info(array1);
console.info(array2);
    
    var unquie = function (array1, array2, name) {
     var myArr = array1.concat(array2);
     var newArr = myArr;
     
     for(var h = 0; h < myArr.length; h++) {
      var curItem = myArr[h][name];
      var foundCount = 0;
          
      for(var i = 0; i < myArr.length; i++) {
       if (myArr[i][name] === myArr[h][name])
        foundCount++;
      }
      if(foundCount > 1) {
       // remove repeated item from new array
       for(var j = 0; j < newArr.length; j++) {
        if(newArr[j][name] === curItem) {
         newArr.splice(j, 1);
         j = j - 1;
        }
       }
      }
     }
    
     return newArr;
    };

    console.info('Converted Arrays');
    console.info(unquie(array1, array2, 'name'));
    //
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
  • So you want array2 to cancel out array1,correct? If something appears in both arrays, drop it from both. Then you'll be combining the results into a single array, right? – DACrosby Jul 29 '15 at 22:35
  • Exactly. If something appears in both arrays remove them. Worth mentioning that I have added the variable `name` because I was to lazy to write something that would check all properties in the object. – Jamie Hutber Jul 29 '15 at 22:36
  • http://stackoverflow.com/questions/1187518/javascript-array-difference or http://stackoverflow.com/questions/21987909/difference-between-two-array-of-objects-in-javascript – epascarello Jul 30 '15 at 14:03

4 Answers4

1

Use filter() to restrict an array to elements missing from the other array.

Use JSON.stringify() to help compare two objects.

Here's a completely functional approach:

function unique(a1, a2) {
  function comp(a1, a2) {
    return a1.filter(function(val1) {
      return !a2.filter(function(val2) {
        return JSON.stringify(val1)==JSON.stringify(val2)
      }).length;
    });
  }
  return comp(a1, a2).concat(comp(a2, a1));
}    

var array1 = 
      [{name: 'placeone', leagueID: '8368223' },
       {name: 'placetwo', leagueID: '6164631' },
       {name: 'placethree', leagueID: '4564836' },
       {name: 'placefour', leagueID: '9722578' },
       {name: 'placefive', leagueID: '9722578' }
      ];

var array2 = 
      [{name: 'placeone', leagueID: '8368223' },
       {name: 'placetwo', leagueID: '6164631' },
       {name: 'placethree', leagueID: '4564836' },
       {name: 'placefour', leagueID: '9722578' },
       {name: 'placesix', leagueID: '9722578' }
      ];

function unique(a1, a2) {
  function comp(a1, a2) {
    return a1.filter(function(val1) {
      return !a2.filter(function(val2) {
        return JSON.stringify(val1)==JSON.stringify(val2)
      }).length;
    });
  }
  return comp(a1, a2).concat(comp(a2, a1));
}

var array3 = unique(array1, array2);
document.body.innerHTML= JSON.stringify(array3);
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
1

Another solution:

var array1 = 
      [ { name: 'placeone', leagueID: '8368223' },
      { name: 'placetwo', leagueID: '6164631' },
      { name: 'placethree', leagueID: '4564836' },
      { name: 'placefour', leagueID: '9722578' },
      { name: 'placefive', leagueID: '9722578' }];

    var array2 = 
      [{name: 'placeone', leagueID: '8368223' },
      {name: 'placetwo', leagueID: '6164631' },
      {name: 'placethree', leagueID: '4564836' },
      {name: 'placefour', leagueID: '9722578' },
      {name: 'placesix', leagueID: '9722578' }]


console.info('Original Arrays');
console.info(array1);
console.info(array2);

    var unquie = function (array1, array2, propName) {
        var myArr = array1.concat(array2);
        var newArr = [];

        for (var i = 0; i < myArr.length; i++) {
          var dupIndex = -1;
          var item = myArr[i];

          for (var j = 0; j < newArr.length; j++) {
            if (item[propName] == newArr[j][propName]) {
              dupIndex = j;
              break;
            }
          }

          if (dupIndex >= 0) {
            newArr.splice(dupIndex, 1);
          } else {
            newArr.push(item);
          }
        }

        return newArr;
    }

    console.info('Converted Arrays');
    console.info(unquie(array1, array2, 'name'));
GPicazo
  • 6,516
  • 3
  • 21
  • 24
1

This may not be the cleanest way, but it works. I loop through the first array, look for the value in the second array and if it isn't found, add it to an output array. Then loop again, this time on the second array

for (var i = 0; i < array1.length; i++) {
    var tmp = array2.filter(function (aa) { return aa.name == array1[i].name });
    if ( !tmp.length )
        array.push( array1[i] )
}
for (var i = 0; i < array2.length; i++) {
    var tmp = array1.filter(function (aa) { return aa.name == array2[i].name });
    if ( !tmp.length )
        array.push( array2[i] )
}

I'm looking for a duplicate name property, but you could compare any property or the object as a whole.

http://jsfiddle.net/daCrosby/j2553xcz/

Here's it's in a function: http://jsfiddle.net/daCrosby/j2553xcz/1/

DACrosby
  • 11,116
  • 3
  • 39
  • 51
0

I proposed another way to do this, instead of removing record from an array if they are duplicated, it would be easier to add it to a new array if it's not already there :

var unquie = function (array1, array2, name) {
    var myArr = array1.concat(array2);
    var newArr = [];

    for(var h = 0; h < myArr.length; h++) {
        var curItem = myArr[h][name];
        var exist = false;
        // search if item already included
        for(var i = 0; i < newArr.length; i++) {
            if (newArr[i][name] === myArr[h][name])
                exist = true;
        }
        if(!exist) {
            // add item if not included
            newArr.push(myArr[h]);
        }
    }

    return newArr;
};
unquie(array1, array2, 'name');
Jitrixis
  • 74
  • 8