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

113
Visualizações
More elegant way to group objects by nested attribute

I am getting the data from a third-party API and cannot filter it beforehand. I have the following array:

[
 [
  User1 {
    attr1: 'abc',
    attr2: ['0x1', '0xa']
  },
  User2 {
    attr1: 'def'
    attr2: ['0x1', '0xb']
  }
 ]
 [
  User3 {
    attr1: 'ghi',
    attr2: ['0x1', '0xa']
  },
  User4 {
    attr1: 'jkl',
    attr2: ['0x1', '0xb']
  },
  User5 {
    attr1: 'mno',
    attr2: ['0x1', '0xc']
  } 
 ]
]

The goal is to get the following. Essentially, I want to group users based on attr2[1] and filter out users that don't have a pair. The key would be the attr2[1].

[
 {
   attr2_1: '0xa',
   attrs: [User1, User3]
 },
 {
   attr2_1: '0xb',
   attrs: [User2, User4]
 }
]

I have a working solution but I would like to know how to solve it more elegantly.

I group the users by the second attribute

const usersByParam = _.chain(allUsers)
       .flatten()
       .groupBy(user => user.attr2[1])
       .value()
   

I filter the grouped users to remove entries that don't have a pair (User5 in the example)

const filteredUsers = _.omitBy(usersByParam, (val) => {return val.length < 2})

I go through the filtered results to get my user objects

for (const key in filteredUsers) {
  userArray.push({attr2_1: key, attrs: filteredUsers[key]} as User)
}

It gives me what I want, but I am sure there are more elegant ways of solving it.

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

0

You can use JavaScript:

const data = [{User1: {attr1: 'abc',attr2: ['0x1', '0xa']}},{User2: {attr1: 'def',attr2: ['0x1', '0xb']}},{User3: {attr1: 'ghi',attr2: ['0x1', '0xa']}},{User4: {attr1: 'jkl',attr2: ['0x1', '0xb']}},{User5: {attr1: 'mno',attr2: ['0x1', '0xc']}}]
const result = Object.entries(
    data.reduce((a, c) => {
      const [k] = Object.keys(c)
      a[c[k].attr2[1]] = [...(a[c[k].attr2[1]] || []), k]
      return a
    }, {})
  )
  .filter(([, users]) => users.length > 1)
  .map(([attr2_1, attrs]) => ({ attr2_1, attrs }))

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

The data that you posted is not valid JSON. So, assuming that it is supposed to be an array of objects, I modified it in my code below to reflect that.

I'm not sure how to do it in lodash, but you can just use vanilla JavaScript to accomplish this. By using the Array.reduce() and Array.filter() methods, you can manipulate the data to how you need.

See snippet below.

const data = [{
    User1: {
      attr1: 'abc',
      attr2: ['0x1', '0xa']
    }
  },
  {
    User2: {
      attr1: 'def',
      attr2: ['0x1', '0xb']
    }
  },
  {
    User3: {
      attr1: 'ghi',
      attr2: ['0x1', '0xa']
    }
  },
  {
    User4: {
      attr1: 'jkl',
      attr2: ['0x1', '0xb']
    }
  },
  {
    User5: {
      attr1: 'mno',
      attr2: ['0x1', '0xc']
    }
  }
];

const reduced = Object.values(data.reduce((a, c) => {
  var user = Object.keys(c)[0];
  var key = c[user].attr2[1];
  a[key] ??= {};
  a[key].attr2_1 = key;
  a[key].attrs ??= [];
  a[key].attrs.push(user);
  return a;
}, {})).filter(element => element.attrs.length === 2);

console.log(reduced);

about 4 years ago · Juan Pablo Isaza Relatório

0

Your code is already elegant, I just replaced the for ... in with map

const userArray = _.chain(allUsers)
  .flatten()
  .groupBy(user => user.attr2[1])
  .omitBy(val => val.length < 2)
  .map((attrs, attr2_1) => ({ attr2_1, attrs }))
  .value();
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