简体   繁体   English

计算具有相同属性值的数组对象

[英]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.在这个修改后的数组中,重复的星期只被放置了一次,并且该值已被特定重复对象中的总值的平均值替换。

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:您可以收集 object 中的值,然后通过将它们的总和除以给定周的可用值数来计算平均值:

 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);

you can do it in simple way like below,你可以像下面这样简单的方式做到这一点,

1.Create a map and added values of similar week and kept their count 2.Then created modified from that. 1.创建一个 map 并添加类似周的值并保持其计数 2.然后创建修改。

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简而言之,我们也可以使用内置函数 forms,但最好理解这个概念

 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 typescript 合并具有相同属性和不同值的数组的对象? - How to merge objects of an array having same property and different values using typescript? 检查对象数组中的所有属性值是否相同 - Check if all property values are the same in an Array of Objects 使用具有相同等级的相同值不可变地对数组中的对象进行排名 - Ranking objects within array immutably with same values having same rank 如何在 javascript 中检索具有相同属性值的对象数组 - How to retrieve the array of objects having same property value in javascript 将具有相同属性的对象数组转换为具有数组值的一个对象 - Convert array of objects with same property to one object with array values 从对象数组中提取具有另一个属性等于给定值的项目的属性值数组 - From array of objects extract array of property values for items having another property equal to given value 检查数组对象的所有属性值在javascript中是否相同 - Checking that all property values of array objects are the same in javascript 对象数组的对象。 获取具有相同属性的所有值的长度 - Object of array of objects. Get the length of all values with the same property 如何通过相同的值和某些属性对对象数组进行联合/分组? - How to union/group array of objects by same values and by some property? 使用相同属性值的数组将多个对象合并为单个对象 - Merge multiple objects into single object with making array of same property values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM