Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!