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

0

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.