1

I have 2 array of objects like

    const arrayOne = [{id: 1, name: 'one'}, {id: 2, name: 'two'}, {id: 3, name: 'three'}];
    const arrayTwo = [{id: 2, name: 'two'}, {id: 3, name: 'three'}];

Here, I need to compare both these arrays and remove matching objects from arrayOne, which should finally give

 this.arrayOne = [{id: 1, name: 'one'}];

I tried like below but it is removing all objects from the array

this.arrayOne = this.arrayOne.filter(o1 => this.arrayTwo.some(o2 => o1.id === o2.id));

What I am doing wrong here? Please suggest. Thanks

4
  • So you just want to see if the ids match? And if they match, remove it from array 1? Commented Feb 14, 2022 at 7:39
  • correct. if ids match i need to remove the object from array 1 Commented Feb 14, 2022 at 7:41
  • OK. I have posted an answer. Please tell if it helps. I am sure there will be a better/smaller way to do it, but that's pretty much the gist of it. Commented Feb 14, 2022 at 7:47
  • Sorry Im late, but what if they keys are not the same for the object? for example, but I want to compare values? ie, key value pairs in two objects, and remove the proeprties whose values match certain conditions? could I use el.key instead of el.id Commented Sep 17, 2022 at 12:10

4 Answers 4

4

const arrayOne = [
  { id: 1, name: "one" },
  { id: 2, name: "two" },
  { id: 3, name: "three" },
];
const arrayTwo = [
  { id: 2, name: "two" },
  { id: 3, name: "three" },
];

const arrayTwoIds = new Set(arrayTwo.map((el) => el.id));
const arrayOneFiltered = arrayOne.filter((el) => !arrayTwoIds.has(el.id));

console.log(arrayOneFiltered);
// [ { id: 1, name: 'one' } ]

Depending on the size of the array, creating a set can improve performance, as you do not need to loop over arrayTwo arrayOne.length times but only once. After that, you can look up the existence of an id in arrayTwo in constant time.

Yet, as pointed out in another answer, this is not necessary if the arrays are small (like in your example). In this case, you could also use this one-liner:

arrayOne = arrayOne.filter((elOne) => !arrayTwo.some((elTwo) => elOne.id === elTwo.id));

Here, arrayOne would need to be mutable, i.e. defined with let.

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

Comments

1

You can find it by comparing it with id.

And arrayOne must be a let.


   let arrayOne = [{id: 1, name: 'one'}, {id: 2, name: 'two'}, {id: 3, name: 'three'}];
   const arrayTwo = [{id: 2, name: 'two'}, {id: 3, name: 'three'}];
   
   arrayOne = arrayOne.filter(one => !arrayTwo.find(two => one.id == two.id));
   
   console.log(arrayOne);


Comments

1

const arrayOne = [{
  id: 1,
  name: 'one'
}, {
  id: 2,
  name: 'two'
}, {
  id: 3,
  name: 'three'
}];

const arrayTwo = [{
  id: 2,
  name: 'two'
}, {
  id: 3,
  name: 'three'
}];
const arrayTwoId = arrayTwo.map(el => (el.id)); // extract id from arrayTwo


const result = arrayOne.filter(el => !arrayTwoId.includes(el.id));
console.log(result);


  1. Extract all the ids from the arrayTwo.
  2. filter those objects who do not match the array of ids of arrayTwo.

4 Comments

Depending on the size of the array, using new Set(arrayTwoId) and .has(el.id) is faster.
fixed with negation.
Looks like someone already posted the one with Set. Just leaving this as is.
Yeah, wasn't a complaint, this is perfectly good enough for short arrays.
0

Your way is correct. but you miss the not operation (!) before arrayTwo.some. So the correct way is this:

const arrayOne = [
  {id: 1, name: 'one'},
  {id: 2, name: 'two'},
  {id: 3, name: 'three'}
];
const arrayTwo = [
  {id: 2, name: 'two'},
  {id: 3, name: 'three'}
];

// Shared Items between arrayOne and arrayTwo (this what you done)
const sharedObjects = arrayOne.filter(o1 => arrayTwo.some(o2 => o1.id === o2.id));

console.log(sharedObjects);

// arrayOne - arrayTwo (this is what you want)
const arrayOneUniqueObjects = arrayOne.filter(o1 => !arrayTwo.some(o2 => o1.id === o2.id));

console.log(arrayOneUniqueObjects);

Also, You can find more details here:

bobbyhadz.com/blog/javascript-get-difference-between-two-arrays-of-objects

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.