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

168
Views
need to group objects, in array of objects, in one group

I have next data:

const data = [
    {amount: 100, percent: 1, days: 7},
    {amount: 75, percent: 3, days: 8},
    {amount: 75, percent: 3, days: 3},
    {amount: 100, percent: 2, days: 5},
    {amount: 100, percent: 1, days: 10},
    {amount: 50, percent: 3, days: 9}
];

The task is to group objects by amount and percent, so that if amount and percent in different objects are the same I need to add their days. The result should look the next way:

const data = [
    {amount: 100, percent: 1, days: 17},
    {amount: 75, percent: 3, days: 11},
    {amount: 100, percent: 2, days: 5},
    {amount: 50, percent: 3, days: 9}
];

1st and 3rd objects are not grouped because of different percent. I have tried to use reduce method:

const result = data.reduce((groupedByAmount, current) => {
    let amount = current.amount;
    let percent= current.percent;
    if (amount == amount && percent == percent) {
        groupedByAmount.amount = amount;
        current.days += current.days
    }
    return groupedByAmount
}, {
    amount: 0,
    percent: null,
    days: 0
});

I don't how to check if amount and percent are the same, maybe map or filter would help

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

0

const data = [
    {amount: 100, percent: 1, days: 7},
    {amount: 75, percent: 3, days: 8},
    {amount: 75, percent: 3, days: 3},
    {amount: 100, percent: 2, days: 5},
    {amount: 100, percent: 1, days: 10},
    {amount: 50, percent: 3, days: 9}
];

const doGroup = (data) => {
    const obj = data.reduce((result, next) => {
    const key = `${next.amount}_${next.percent}`
    const dd =  result[key]?.days ?? 0;
    result[key] = {...next, days: next.days + dd};
    return result;
  }, {});
  return Object.values(obj);
}

console.log(doGroup(data))

about 4 years ago · Juan Pablo Isaza Report

0

Considering each of the methods you've suggested:

  • reduce => reduce array elements to a single value. Can be used to create an object, then convert that object back to the array

  • map => return a value for every value in the array

  • filter=> return multiple (or single) matching values

You can loop through each value, building a new array and using filter() to find existing values to add to.

Note: this likely won't perform well with large arrays due to the multiple use of .filter() - using a composite key (combining two values into a single value) would likely perform better.

const data = [
    {amount: 100, percent: 1, days: 7},
    {amount: 75, percent: 3, days: 8},
    {amount: 75, percent: 3, days: 3},
    {amount: 100, percent: 2, days: 5},
    {amount: 100, percent: 1, days: 10},
    {amount: 50, percent: 3, days: 9}
];

var result = [];

data.forEach(e => {
    var current = result.filter(d => d.amount == e.amount && d.percent == e.percent);
    if (current.length === 0) {
        result.push(e);
    } else {
        current[0].days += e.days;
    }
});
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

const data = [
    {amount: 100, percent: 1, days: 7},
    {amount: 75, percent: 3, days: 8},
    {amount: 75, percent: 3, days: 3},
    {amount: 100, percent: 2, days: 5},
    {amount: 100, percent: 1, days: 10},
    {amount: 50, percent: 3, days: 9}
];

const temp = data.reduce((acc, {amount, percent, days}) => {
  acc[amount] ||= {}
  acc[amount][percent] ||= 0
  acc[amount][percent] += days
  return acc;
}
,{});

const result = Object.entries(temp).reduce((acc, [amount, obj]) => {
  acc.push(...Object.entries(obj).map(([percent, days]) => ({ amount: Number(amount), percent: Number(percent), days })))
  return acc
},[])

console.log(result)

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!