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

288
Visualizações
lodash unique based on everything except attribute

This is the opposite requirement to lodash unique based on attribute, here I want to make the array unique by ignoring one (or more) attribute.

So in the below, I don't care about the 'age' attribute (maybe there's multiple records, and I want to only keep where the details change).

const arr = [{
  "id": 1,
  "name": "Jen",
  "age": 31,
  "eyecolor": "blue",
  "hair": "brown",
}, {
  "id": 1,
  "name": "Jen",
  "age": 32,
  "eyecolor": "blue",
  "hair": "brown"
}, {
  "id": 2,
  "name": "Jules",
  "age": 31,
  "eyecolor": "blue",
  "hair": "brown"
}, {
  "id": 2 "name": "Brian",
  "age": 40,
  "eyecolor": "blue",
  "hair": "brown"
}];

const unique = _.uniqBy(arr, 'id');

console.log(unique);

// const uniqueNot = _.uniqExcept(arr, 'age');
// keeps one of the Jen objects, but both of the Jules/Brian objects
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

I want the above, as the comment says, to keep one of the Jen objects, but both of the Jules/Brian objects.

I think I can do this like so:

const arr = [{
  "id": 1,
  "name": "Jen",
  "age": 31,
  "eyecolor": "blue",
  "hair": "brown",
}, {
  "id": 1,
  "name": "Jen",
  "age": 32,
  "eyecolor": "blue",
  "hair": "brown"
}, {
  "id": 2,
  "name": "Jules",
  "age": 31,
  "eyecolor": "blue",
  "hair": "brown"
}, {
  "id": 2,
  "name": "Brian",
  "age": 40,
  "eyecolor": "blue",
  "hair": "brown"
}];

const omissionComparator = (withoutKeys) => (a, b) => _.isEqual(_.omit(a, withoutKeys), _.omit(b, withoutKeys))

const  uniqueWithoutAge = _.uniqWith(_.orderBy(arr, ['id','age'], ['asc', 'desc']), omissionComparator('age'));

console.log(uniqueWithoutAge);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

But is there a way of doing this without a custom comparator and/or having to sort the array first?

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

0

Maybe this? uniqBy + omit + JSON.stringify

const arr = [{
  "id": 1,
  "name": "Jen",
  "age": 31,
  "eyecolor": "blue",
  "hair": "brown",
}, {
  "id": 1,
  "name": "Jen",
  "age": 32,
  "eyecolor": "blue",
  "hair": "brown"
}, {
  "id": 2,
  "name": "Jules",
  "age": 31,
  "eyecolor": "blue",
  "hair": "brown"
}, {
  "id": 2,
  "name": "Brian",
  "age": 40,
  "eyecolor": "blue",
  "hair": "brown"
}];

const keysToIgore = ['age'];

const result = _.uniqBy(arr, o => JSON.stringify(_.omit(o, keysToIgore)));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

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