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

97
Views
Calculation on array objects having same property values

I have an array as-

const a = [
  {
    value: 1,
    week: 'week1',
  },
  {
    value: 2,
    week: 'week1',
  },
  {
    value: 3,
    week: 'week16',
  },
  {
    value: 4,
    week: 'week0',
  },
  {
    value: 5,
    week: 'week16',
  },
]

I want to have a modified array in the following way-

let modified = [
  {
    value: 1.5,
    week: 'week1',
  },
  {
    value: 4,
    week: 'week16',
  },
  {
    value: 4,
    week: 'week0',
  },
]

In this modified array, the duplicate week has been put only once and the value has been replaced by average of total value in the particular duplicate objects.

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

0

You can collect the values in an object, and then calculate the average by dividing their sum over the number of available values for a given week:

const a = [{
  value: 1,
  week: 'week1',
}, {
  value: 2,
  week: 'week1',
}, {
  value: 3,
  week: 'week16',
}, {
  value: 4,
  week: 'week0',
}, {
  value: 5,
  week: 'week16',
}];

const result = Object.entries(
  a.reduce((a, {value, week}) => ({
    ...a,
    [week]: [...a[week] || [], value]
  }), {})
).map(([week, values]) => ({
  week,
  value: values.reduce((a, v) => a + v, 0) / values.length
}));

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

you can do it in simple wat like below,

1.Create a map and added values of similar week and kept their count 2.Then created modified from that.

const a = [
    {
        value: 1,
        week: 'week1',
    },
    {
        value: 2,
        week: 'week1',
    },
    {
        value: 3,
        week: 'week16',
    },
    {
        value: 4,
        week: 'week0',
    },
    {
        value: 5,
        week: 'week16',
    },
];


const helperMap = {}

a.forEach((obj) => {
    if (!!helperMap[obj.week]) {
        helperMap[obj.week]['sum'] += obj.value;
        helperMap[obj.week]['count'] += 1;
    } else {
        helperMap[obj.week] = { sum: obj.value, count: 1 };
    }
});

const modified = [];
for (let week in helperMap) {
    let avgValue = helperMap[week]['sum'] / helperMap[week]['count'];
    modified.push({ week, value: avgValue });
}

console.log(modified)

We can do it in short forms using inbuilt functions also, but it will be better to understand the concept

about 4 years ago · Juan Pablo Isaza Report

0

const a = [
    {
        value: 1,
        week: 'week1',
    },
    {
        value: 2,
        week: 'week1',
    },
    {
        value: 3,
        week: 'week16',
    },
    {
        value: 4,
        week: 'week0',
    },
    {
        value: 5,
        week: 'week16',
    },
]

let modified = [];
a.forEach(item => {
    let match = modified.find(m => m.week === item.week);
    if (match) match.value = (match.value + item.value);
    else modified.push(item);
});
modified.forEach(m => m.value =  m.value / a.filter(a => a.week === m.week).length);
console.log(modified)

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!