1

Here is my problem statement.

I have two arrays

assignedArr = [{ id: 'abc1' }, { id: 'abc2' }, { id: 'abc3' }];

 unAssignedArr = [{ id: 'abc1' }, { id: 'abc2' }, { id: 'abc3'}, { id: 'abc4' }];

I have to compare assingedArr with unAssignedArr, and return the unmatched array item.

For example, In the above example,

the result should be

   newArr = [{ id: 'abc4' }];

Like soo...

Any suggestions would greatly appriaciated

imLohith
  • 219
  • 2
  • 3
  • 15

1 Answers1

1

You could use array Array.prototype.filter with Array.prototype.some method to get your result.

const assignedArr = [{ id: 'abc1' }, { id: 'abc2' }, { id: 'abc3' }];

const unAssignedArr = [
  { id: 'abc1' },
  { id: 'abc2' },
  { id: 'abc3' },
  { id: 'abc4' },
];

const ret = [
  ...unAssignedArr.filter((x) => !assignedArr.some((y) => x.id === y.id)),
  ...assignedArr.filter((x) => !unAssignedArr.some((y) => x.id === y.id)),
];
console.log(ret);
mr hr
  • 3,162
  • 2
  • 9
  • 19