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

141
Visualizações
I want to compare two json objects excluding one of the keys from it

I have two objects x,y and i want to compare both of these excluding one of the keys "c"

let x = {a: 5, b: 6, c: "string"}
let y = {a: 5, b: 8, c: "string"}

How i am trying to compare it is -

JSON.stringify(x) === JSON.stringify(y)

Above works but it will compare all keys I want to exclude c in comparison here. What's the best way to achieve this ?

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

0

The following code obtains all the keys from object x, removes c from the list, and then performs the comparison.

let x = { a: 5, b: 6, c: "string" };
let y = { a: 5, b: 6, c: "another string" };

console.log(
  Object.keys(x)
    .filter((key) => key !== "c")
    .every((key) => x[key] === y[key])
);
about 4 years ago · Juan Pablo Isaza Relatório

0

array.sort requires a comparator function. You can use the exact same thing here

function myObjComparator (x, y) {
  if (x.a != y.a) return y.a - x.a;
  return y.b - x.b;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Generally, I would NOT stringify objects in order to compare them. The reason is quite simple and is that you MUST to be sure that the order of the members are the same, otherwise the result won't be correct.

Ex:

// objects are equals
const y = { a: '1', b: '2' }
const x = { b: '2', b: '1' }
// but result is not what you expect
JSON.stringify(x) === JSON.stringify(y) // false

But, if you can ensure that the order of the members is always the same, stringify is fast and a good option.

You can use the spread syntax in order to avoid the "c" property

const remappedX = { ...x, c: undefined };
const remappedY = { ...y, c: undefined };
JSON.stringify(remappedX) === JSON.stringify(remappedY); // true

Or alternatively

const allowedKeys = Object.keys(x).filter((k) => k !== 'c');
JSON.stringify(x, allowedKeys) === JSON.stringify(y, allowedKeys);

More generic method is to loop over Object key or alternatively to entries

for(const key of Object.keys(x)) {
   if (key === 'c') {
      continue;
   }
   if (x[key] !== y[key]) {
         return false;
   }
}
return true;

But, if your object is nested, you need to use a deep equality algorithm, which is of course slower.

More generic answer has been given here

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