Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda