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

127
Views
Ramda: group by nested object

I have this data:

const entities = [
  { id: 'view_1', group: { id: 'g_1', name: 'Group 1' } },
  { id: 'view_2', group: { id: 'g_2', name: 'Group 2' } },
  { id: 'view_3', group: { id: 'g_2', name: 'Group 2' } },
];

And I need to get this as output:

const groupedEntities = [
  { id: 'g_1', name: 'Group 1', views: [{ id: 'view_1' }] },
  { id: 'g_2', name: 'Group 2', views: [{ id: 'view_3' }, { id: 'view_3' }] },
];

I know there is indexBy in ramda but it does not preserve other data than key. What I need is to preserve all data and return an object.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ramda's groupBy can help you here:

const transform = pipe (
  groupBy (path (['group', 'id'])),
  values,
  map (applySpec ({
    group: path ([0, 'group', 'id']),
    name: path ([0, 'group', 'name']),
    views: pluck ('id')
  }))
)

const entities = [{id: 'view_1', group: { id: 'g_1', name: 'Group 1'}}, {id: 'view_2', group: { id: 'g_2', name: 'Group 2'}}, {id: 'view_3', group: { id: 'g_2', name: 'Group 2'}}]

console .log (transform (entities))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
<script>const {pipe, groupBy, path, values, map, applySpec, pluck} = R</script>     

The rest of it is a bit more of a handful because of the need to pull parts from the first element in each group and parts from the whole group, but overall, it's not too bad:

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!