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

180
Visualizações
Best way to find the difference between 2 arrays of objects

I have these 2 arrays of objects.

enter image description here

Sorry that I post the image because I want you guys to quickly understand my points.

What is the best way to compare 2 arrays of objects and look for what is not in it?

I can do 2 levels for loops and an if-check, but I'm not sure if it is the best approach. I am seeking a better performance JS way to achieve something like this.

This is what I am looking to extract while comparing arr1 & arr2.

{
    "id": 4,
    "name": "Date and Time"
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Different ways to achieve this requirement. I also created a performance check based on all the below 3 solutions here. Kindly run this test suite and based on the result you can use any of the below solution in your project.

  1. Filtered out the array by using Array.filter() method and find the id in the another array by using Array.find() method.

const arr1 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}];

const arr2 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}, {
    id: 4,
  name: "Date and Time"
}];

const res = arr2.filter((obj) => !arr1.find(({ id }) => obj.id === id));

console.log(res);

  1. By using Array.includes() method.

const arr1 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}];

const arr2 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}, {
    id: 4,
  name: "Date and Time"
}];

const IDArray = arr1.map(obj => obj.id);

const res = arr2.filter(obj => !IDArray.includes(obj.id));

console.log(res);

  1. Use Array.some() along with Array.filter() method.

const arr1 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}];

const arr2 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}, {
    id: 4,
  name: "Date and Time"
}];

const res = arr2.filter(({ id }) => !arr1.some(x => x.id == id))

console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório

0

One way is to concatenate both arrays together, then iterate and filter to see if that id exists in both arrays. If it doesn't exist in both arrays, it's extra.

a1.concat(a2).filter(a => a1.findIndex(f => f.id === a.id) == -1 || a2.findIndex(f => f.id === a.id) == -1)

let arr1 = [{"id": 1,"name": "Date and Time"},{"id": 2,"name": "Date and Time"},{"id": 3,"name": "Date and Time"},{"id": 4,"name": "Date and Time"}]

let arr2 = [{"id": 1,"name": "Date and Time"},{"id": 2,"name": "Date and Time"},{"id": 3,"name": "Date and Time"}]

const findMissing = (a1, a2) => a1.concat(a2).filter(a => a1.findIndex(f => f.id === a.id) == -1 || a2.findIndex(f => f.id === a.id) == -1)

console.log(findMissing(arr1, arr2))

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