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

181
Views
How do I sum a key and group by value using Ramda?

I have data like this:

const items = [
  {category: 'food', amount: 5},
  {category: 'food', amount: 5},
  {category: 'transport', amount: 2},
  {category: 'travel', amount: 11},
  {category: 'travel', amount: 1},
]

How can I sum amount and group by each category yielding:

{
  food : 10,
  transport : 2,
  travel : 12,
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could use reduceBy:

R.reduceBy((acc, next) => acc + next.amount, 0, R.prop('category'))(items);
about 4 years ago · Juan Pablo Isaza Report

0

Using reduceBy is probably a good idea. Here's an alternative in case you find it useful.

Start with an itemize function:

const itemize = ({category: c, amount: a}) => ({[c]: a});

itemize({category: 'food', amount: 5});
//=> {food: 5}

Then define a function that can either add or merge items:

const additem = mergeWith(add);

additem({food: 5}, {food: 5});
//=> {food: 10}

additem({food: 5}, {transport: 2});
//=> {food: 5, transport: 2}

Finally use these two functions in a reducer:

reduce((a, b) => additem(a, itemize(b)), {}, items);
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!