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

187
Visualizações
JS find array of String and Array Object without duplicates

I had a lists of duplicate array of String and array of object. I want to find the property into one particular object of array uniquely without duplication of array object. If can done in lodash library, it would be awesome.

const arr1 = ['test@email', 'test2@email']
const arr2 = [{ id: 1, email: 'test@email' }]

Expected result

['test2@email']

this is what I done so far By turning arr1 into object frist so I can compare with arr2

 const emailLists = _.map(arr, function (item) {
    console.log(item)
    return { email: item }
  })

then merge them to together and used uniq to remove duplicates

 const merge = _.unionBy(array1, array2, 'email')
 const result _.uniq(merge, 'email');

I think it still not a good process and not clean

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

0

You can do it without lodash

const arr1 = ['test@email', 'test2@email']
const arr2 = [{ id: 1, email: 'test@email' }]


const emailToDelete = arr2.map(a => a.email)

const result = arr1.filter(e => !emailToDelete.includes(e))

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use lodash chain to solve that:

const result = _([...arr1, ...arr2.map(i => i.email)]) // ['test@email', 'test2@email', 'test@email']
    .countBy() // { test@email: 2, test2@email: 1 }
    .pickBy(count => count === 1) // { test2@email: 1 }
    .keys() // ['test2@email']
    .value();
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use filter on the first array and check that an email address matches with some of your objects in the second array. So even if it matches with multiple objects, the matching email will only be in the output once.

I want to find the property into one particular object of array uniquely without duplication of array object

So then your expected output is wrong, as you provide an email that is not found in the object array. I guess that was a typo in your question.

const arr1 = ['test@email', 'test2@email']
const arr2 = [{ id: 1, email: 'test@email' }]

const result = arr1.filter(search => arr2.some(({email}) => email == search));

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