Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda