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

188
Visualizações
Given two equal objects, filter only the differences

Lets say I have two different objects

let obj1 = {
  "value1" : "1",
  "value2" : "2",
  "value3" : "3",
  "value4" : "4"
}

let obj2 = {
  "value1" : "1",
  "value2" : "3",
  "value3" : "3",
  "value4" : "5"
}

I need a the differences in a new object that will depend on obj2

result = { "value2":"3" , "value4" : "5" }

The case can be too , that obj1 === undefined In that case, I need all the values from obj2

Currently I have

let result = {}
let singleChange = false

for (const i of Object.entries(obj2)) {
    if (obj1 && obj1[i[0]] !== i[1]) {
      result [i[0]] = i[1];
      singleChange = true
    } else if(!singleChange) {
      result = obj2;
    }
  }

But it doesnt really sem to work properly all the times. Is there a better way to write this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

A modern version with entries and fromEntries:

let obj1 = {
  "value1" : "1",
  "value2" : "2",
  "value3" : "3",
  "value4" : "4"
}

let obj2 = {
  "value1" : "1",
  "value2" : "3",
  "value3" : "3",
  "value4" : "5"
}

let obj3 = undefined;

function objIntersection(o1, o2) {
  const filteredArray = Object.entries(o2)
  .filter(([k,v]) => o1?.[k] !== v);

  return Object.fromEntries(filteredArray);
}

console.log(objIntersection(obj1, obj2));
console.log(objIntersection(obj3, obj2));

about 4 years ago · Juan Pablo Isaza Relatório

0

const obj1 = {
  value1: '1',
  value2: '2',
  value3: '3',
  value4: '4',
};

const obj2 = {
  value1: '1',
  value2: '3',
  value3: '3',
  value4: '5',
};
// find only the values that are different
const diff = (obj1, obj2) => {
  const obj3 = {};
  for (const key in obj1) {
    if (obj1[key] !== obj2[key]) {
      obj3[key] = obj2[key];
    }
  }
  return obj3;
}
console.log(diff(obj1, obj2));

about 4 years ago · Juan Pablo Isaza Relatório

0

You could get the entries of the second object and filter with the first one.

const
    getDifference = (a, b) => Object.fromEntries(Object
        .entries(b)
        .filter(([k, v]) => a[k] !== v)
    ),
    obj1 = { value1 : "1", value2 : "2", value3 : "3", value4 : "4" },
    obj2 = { value1 : "1", value2 : "3", value3 : "3", value4 : "5" },
    result = getDifference(obj1, obj2);

console.log(result);

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