4

I've found a lot of similar questions but they explain how to remove duplicate objects. In this case, I need to create a new array that doesn't include objects which were found in two arrays.

const firstArray = [
  {firstName: 'John', lastName: 'Doe'}, 
  {firstName: 'Sara', lastName: 'Connor'},
  {firstName: 'Mike', lastName: 'Hunt'}, 
  {firstName: 'Steve', lastName: 'Irvine'}
];

const secondArray = [ 
  {firstName: 'John', lastName: 'Doe'},  
  {firstName: 'Sara', lastName: 'Connor'} 
];

The expected result for the previous sample of data should be:

const result = [
  {firstName: 'Mike', lastName: 'Hunt'},
  {firstName: 'Steve', lastName: 'Irvine'}
];
Shidersz
  • 16,846
  • 2
  • 23
  • 48
  • 2
    What is your question? Have you made an attempt at solving this on your own? By giving us solely input and output, the implied question is "I need code that does this, someone please write it for me". If you have an attempt of your own, or perhaps some research, it should likely lead you to a *specific* question - *that* question is the one you should be asking :) – Tyler Roper Feb 25 '19 at 15:13
  • 1
    Possible duplicate: [How to compare two arrays and remove duplicate objects by complete iteration](https://stackoverflow.com/questions/49557351/how-to-compare-two-arrays-and-remove-duplicate-objects-by-complete-iteration), [Remove duplicates in an object array Javascript](https://stackoverflow.com/questions/36032179/remove-duplicates-in-an-object-array-javascript), [Comparing arrays of objects and remove duplicate](https://stackoverflow.com/questions/47789181/comparing-arrays-of-objects-and-remove-duplicate) – Tyler Roper Feb 25 '19 at 15:20

4 Answers4

2

I will first concatenate both arrays and then filter() those elements that don't appear on both arrays using some() for search on each of the arrays. Note this approach take into account the possibility that both the firstArray and the secondArray can contain non-duplicated objects.

const firstArray = [
  {firstName: 'John', lastName: 'Doe'}, 
  {firstName: 'Sara', lastName: 'Connor'},
  {firstName: 'Mike', lastName: 'Hunt'}, 
  {firstName: 'Steve', lastName: 'Irvine'}
];

const secondArray = [ 
  {firstName: 'John', lastName: 'Doe'},  
  {firstName: 'Sara', lastName: 'Connor'},
  {firstName: 'Joseph', lastName: 'Muguera'}
];

let res = firstArray.concat(secondArray).filter(({firstName, lastName}) =>
{
    let foundOnFirst = firstArray.some(
        x => x.firstName === firstName && x.lastName === lastName
    );
    let foundOnSecond = secondArray.some(
        y => y.firstName === firstName && y.lastName === lastName
    );
    return !(foundOnFirst && foundOnSecond);
});

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
Shidersz
  • 16,846
  • 2
  • 23
  • 48
1

You could filter firstArray by iterating secondArray and check the wanted properties for compairing.

const
    firstArray = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Sara', lastName: 'Connor' }, { firstName: 'Mike', lastName: 'Hunt' }, { firstName: 'Steve', lastName: 'Irvine' }],
    secondArray = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Sara', lastName: 'Connor' }],
    result = firstArray.filter(o => 
        secondArray.every(p =>
            !['firstName', 'lastName'].some(k => o[k] === p[k])));

console.log(result);

An approach with a Set of joined properties.

const
    firstArray = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Sara', lastName: 'Connor' }, { firstName: 'Mike', lastName: 'Hunt' }, { firstName: 'Steve', lastName: 'Irvine' }],
    secondArray = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Sara', lastName: 'Connor' }],
    getKey = ({ firstName, lastName }) => [firstName, lastName].join('|'),
    secondSet = new Set(firstArray.map(getKey)),
    result = firstArray.filter(o => !set.has(getKey(o)));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You can filter one array and compare object with JSON.stringify with second array,

const firstArray = [
    {firstName: 'John', lastName: 'Doe'}, 
    {firstName: 'Sara', lastName: 'Connor'},
    {firstName: 'Mike', lastName: 'Hunt'}, 
    {firstName: 'Steve', lastName: 'Irvine'}
    ];

const secondArray = [ 
    {firstName: 'John', lastName: 'Doe'},  
    {firstName: 'Sara', lastName: 'Connor'} 
    ];
    
const res=firstArray.filter(function(obj) {
 // JSON.stringify(obj)==JSON.stringify(obj)
  for( var i=0, len=secondArray.length; i<len; i++ ){
          if(JSON.stringify(obj)==JSON.stringify(secondArray[i]) ) {
              return false;
          }
      }
 return true; 
});
console.log(res);
Vikas
  • 6,868
  • 4
  • 27
  • 41
0

An alternative approach can be based on .findIndex() and JSON.stringify():

const firstArray = [
    {firstName: 'John', lastName: 'Doe'},
    {firstName: 'Sara', lastName: 'Connor'},
    {firstName: 'Mike', lastName: 'Hunt'},
    {firstName: 'Steve', lastName: 'Irvine'}
];

const secondArray = [
    {firstName: 'John', lastName: 'Doe'},
    {firstName: 'Sara', lastName: 'Connor'}
];
const result = firstArray.filter(ele =>
    secondArray.findIndex(ele1 =>
        JSON.stringify(ele1) == JSON.stringify(ele)) == -1
    );

console.log(result);
gaetanoM
  • 41,594
  • 6
  • 42
  • 61