Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Compare two array of objects and remove objects from first array if a property value is matched

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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


about 4 years ago · Juan Pablo Isaza Report

0

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.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!