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

175
Visualizações
how to check for same objects in two different arrays in JavaScript

i want to do something like this,

let array1 = [{obj1}, {obj2},{obj3}] 
let array2 = [{obj1}, {obj4},{obj5}]

output should be like

{obj1}

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

0

This could work for simple objects.

Take in mind that it will not work for functions based properties.

const array1 = [{a:1}, {b:2},{c:3}] 
const array2 = [{a:1}, {d:4},{e:5}]

const array1Stringify = array1.map(el => JSON.stringify(el));
const array2Stringify = array2.map(el => JSON.stringify(el));

const result = array1Stringify.filter(el => array2Stringify.includes(el)).map(el => JSON.parse(el));

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

As commented, most of your problem is to define how your objects equality will be evaluated. Once you get that resolved, simply checking for the matches of one array in the other shold give you the matches you are after. Most readable and naive way, with a double for.

let array1 = ['hello', 'world', 'I rule'] 
let array2 = ['hello', 'whatever', 'hey brother']

let matches = [];
for (let i = 0; i < array1.length; i++) {
    for (let j = 0; j < array2.length; j++) {
        if (array1[i] === array2[j]) { //equality for object problem here
            matches.push(array1[i]);
        }
    }
}

console.log({matches});
about 4 years ago · Juan Pablo Isaza Relatório

0

  • Try this answer but you'll need an id or an specific object key value to compare https://stackoverflow.com/a/42995029/12128519
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