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

260
Visualizações
How to compare 2 different objects with different fields and create a new object with a new field that tells common elements

I have

obj1= [{keyID: 'c71cc89335565241f59ac9bb9da162a3ddf4a6b483bdc06dd82b9275898ba5ee', name: 'user1@mail.com'},
 {keyID: '34051d2554ab20b0d24c2c39946d96da7e07508e7a8546bc2df299601dcbff05', name: 'user2@mail.com'}]

And

obj2=[{id: 19, email: 'user4@mail.com', other_field: false},
  {id: 221, email: 'user3@mail.com', other_field: false},
{id: 13, email: 'user2@mail.com', other_field: false)},
 {id: 2, email: 'user1@mail.com', other_field: true]

As you can see I have for obj1 keyID that is diffenret from obj2 ID and that is fine. The thing that I want to group them by is name(obj1) and email(obj2) the field with the same email should create a new object with the same structure obj2 with a new field "isPresent"

result=[{id: 19, email: 'user4@mail.com', other_field: false,isPresent=false},
  {id: 221, email: 'user3@mail.com', other_field: false,isPresent=false},
{id: 13, email: 'user2@mail.com', other_field: false,isPresent=true)},
 {id: 2, email: 'user1@mail.com', other_field: true,isPresent=true]

How can I achieve that?

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

0

You can map over obj2 and check if an element with the same name exists in obj1 via some.

I also spread the obj inside the map instead of assigning isPresent directly to avoid side effects.

const obj1 = [{ keyID: 'c71cc89335565241f59ac9bb9da162a3ddf4a6b483bdc06dd82b9275898ba5ee', name: 'user1@mail.com' }, { keyID: '34051d2554ab20b0d24c2c39946d96da7e07508e7a8546bc2df299601dcbff05', name: 'user2@mail.com' }];
const obj2 = [{ id: 19, email: 'user4@mail.com', other_field: false }, { id: 221, email: 'user3@mail.com', other_field: false }, { id: 13, email: 'user2@mail.com', other_field: false }, { id: 2, email: 'user1@mail.com', other_field: true }];
const result = obj2.map((obj) => ({
  ...obj,
  isPresent: obj1.some(({ name }) => name === obj.email),
}));
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