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

196
Visualizações
Check if two arrays of objects have differences isolating the latter

I have two arrays of objects. Each object will have a unique property id which, for context, is a filterID retrieved from a dynamoDb database. What I want to achieve is, retrieving an array of the objects that are different given the same id.

const x = [
    {
        1234: "food"
    },
    {
        5678: "animal"
    },
    {
        4444: "car"
    }
]

const y = [
    {
        1234: "food"
    },
    {
        5678: "bread"
    },
    {
        4444: "car"
    }
]

const p = () => x.filter(object1 => {
    return !y.some(object2 => {
        return object1 === object2
    })
})
 console.log(p())

In this scenario I would be looking for something like

const different = [
    {
        5678: "animal"
    },
]

I have tried a few things like on the example above but so far no success.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You're close, you just need to get the object key and compare the values, instead of comparing the object references.

const p = () => x.filter(object1 => {
    return !y.some(object2 => {
        const key = Object.keys(object1)[0];
        return object1[key] == object2[key];
    })
})
 console.log(p())
about 4 years ago · Santiago Trujillo Relatório

0

use this code instead:

const p = () =>
  x.filter((object1) => {
    return !y.some((object2) => {
      return JSON.stringify(object1) === JSON.stringify(object2);
   });
  });
console.log(p());

you cannot compare two objects because they always will be different because they are at different location in memory, you have to compare their properties or convert them to string and compare them

about 4 years ago · Santiago Trujillo Relatório

0

const x = [
    {
        1234: "food"
    },
    {
        5678: "animal"
    },
    {
        4444: "car"
    }
]

const y = [
    {
        1234: "food"
    },
    {
        5678: "bread"
    },
    {
        4444: "car"
    }
]

const findByPropertyDiff = (a,b)=>{
   let rs = a.filter(item=>{
     return Array.from(Object.keys(item)).some(k=>{
      return b.some(compared=>{
         return (compared[k] && item[k] != compared[k]);
      });       
     })
   });
   return rs;  
}

const updatedItem = findByPropertyDiff(x,y);
console.log(updatedItem);

about 4 years ago · Santiago Trujillo 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