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

252
Visualizações
Compare 2 difference JSON array object

as you can see i have 2 arrays

var obj1 = [
    "planet-1",
    "planet-2",
    "planet-3",
    "planet-4"
]

and

var obj2 = [
    "planet-1",
    "planet-8"
]

i was find something like console.log(obj1 == obj2) will return true using stringify, that is almost close from i need but the problem is my json data not have exactly the same between obj1 and obj2, so when i use code like console.log(obj1) == obj2) the return will always false because of json data not have exactly the same.

so can someone please to help me how to figure it out how to make the result will return true if my "obj2" have atleast 1 or 2 data that the same like "obj1" even i have 1 array that not available in obj1?

im sorry i cant show any code for reference, cause i just find console.log(obj1) == obj2) who really close to what im lookin for.

thanks for helping.

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

0

try this

let difference = obj1.filter(x => !obj2.includes(x));
console.log(difference); // ["planet-2", "planet-3"]

//or

console.log(difference.length > 0); //true

or if you don't know which array is bigger, you can use this code

const isDifferent = function (arr1, arr2) {
  let difference = false;
  if (arr1.length > arr2.length)
    difference = arr1.filter((x) => !arr2.includes(x));
  else difference = arr2.filter((x) => !arr1.includes(x));
  return difference.length > 0;
};

console.log(isDifferent(obj2,obj1)); // true
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