【问题标题】:How to calculate average of values with duplicate key in array of objects with ES6? [duplicate]如何使用 ES6 计算对象数组中具有重复键的值的平均值? [复制]
【发布时间】:2021-04-30 12:48:20
【问题描述】:

我有一个这样的对象数组。

[{"x":"Apr","y":34},{"x":"May","y":34},{"x":"May","y":30},{"x":"May","y":33},{"x":"May","y":26},{"x":"May","y":32},{"x":"May","y":29},{"x":"May","y":32},{"x":"May","y":24},{"x":"May","y":46},{"x":"May","y":43},{"x":"May","y":46},{"x":"May","y":41},{"x":"May","y":40},{"x":"May","y":37},{"x":"May","y":40},{"x":"May","y":40},{"x":"May","y":42},{"x":"May","y":41},{"x":"May","y":43},{"x":"May","y":43},{"x":"May","y":42},{"x":"May","y":42},{"x":"May","y":44},{"x":"May","y":44},{"x":"May","y":46},{"x":"May","y":46},{"x":"May","y":47},{"x":"May","y":47},{"x":"May","y":48},{"x":"May","y":48},{"x":"May","y":48},{"x":"Jun","y":48},{"x":"Jun","y":49},{"x":"Jun","y":49}]

如果键是重复的,那么所有值将被平均如下。

[{"x":"Apr","y":34},{"x":"May","y":41.5},{"x":"Jun","y":46.2}]

我已尝试参考此链接,但它不起作用。 Calculate average of duplicates in a javascript array of objects

【问题讨论】:

  • 您能否详细说明您尝试使用的解决方案如何不起作用?你收到错误了吗?阵列没有减少吗?平均值是否输出为非平均值?如果您编辑了问题并包含了您尝试实现的代码,这也会有所帮助。

标签: javascript arrays ecmascript-6


【解决方案1】:

const arr = [{"x":"Apr","y":34},{"x":"May","y":34},{"x":"May","y":30},{"x":"May","y":33},{"x":"May","y":26},{"x":"May","y":32},{"x":"May","y":29},{"x":"May","y":32},{"x":"May","y":24},{"x":"May","y":46},{"x":"May","y":43},{"x":"May","y":46},{"x":"May","y":41},{"x":"May","y":40},{"x":"May","y":37},{"x":"May","y":40},{"x":"May","y":40},{"x":"May","y":42},{"x":"May","y":41},{"x":"May","y":43},{"x":"May","y":43},{"x":"May","y":42},{"x":"May","y":42},{"x":"May","y":44},{"x":"May","y":44},{"x":"May","y":46},{"x":"May","y":46},{"x":"May","y":47},{"x":"May","y":47},{"x":"May","y":48},{"x":"May","y":48},{"x":"May","y":48},{"x":"Jun","y":48},{"x":"Jun","y":49},{"x":"Jun","y":49}];

const averages = [...arr
  // get list of month/values
  .reduce((map, { x, y }) => map.set(x, [...(map.get(x) || []), y]), new Map) ]
  // get list of month/average
  .map(([x, y]) => ({ x, y: y.reduce((sum, val) => sum + val, 0) / y.length }));
  
  
console.log(averages);

【讨论】:

    【解决方案2】:

    您可以使用Array.reduceObject.valuesArray.map,如下所示

    let data = [{"x":"Apr","y":34},{"x":"May","y":34},{"x":"May","y":30},{"x":"May","y":33},{"x":"May","y":26},{"x":"May","y":32},{"x":"May","y":29},{"x":"May","y":32},{"x":"May","y":24},{"x":"May","y":46},{"x":"May","y":43},{"x":"May","y":46},{"x":"May","y":41},{"x":"May","y":40},{"x":"May","y":37},{"x":"May","y":40},{"x":"May","y":40},{"x":"May","y":42},{"x":"May","y":41},{"x":"May","y":43},{"x":"May","y":43},{"x":"May","y":42},{"x":"May","y":42},{"x":"May","y":44},{"x":"May","y":44},{"x":"May","y":46},{"x":"May","y":46},{"x":"May","y":47},{"x":"May","y":47},{"x":"May","y":48},{"x":"May","y":48},{"x":"May","y":48},{"x":"Jun","y":48},{"x":"Jun","y":49},{"x":"Jun","y":49}]
        
    
    const formatData = (data) => {
      return Object.values(data.reduce((res, obj) => {
        //check if the object already exists in the res object
        //if already available then add the y value of the current
        //object to the y value of the object present in res
        if (res[obj.x]) {
          res[obj.x].y += obj.y;
          res[obj.x].count += 1;
        } else {
        //If the object is not present then add the current object and the count as 1
          res[obj.x] = { 
            ...obj,
            count: 1
          };
        }
        return res
        //then loop through the array and calculate the avg
      }, {})).map(obj => ({
        x: obj.x,
        y: obj.y / obj.count
      }));
    }
    console.log(formatData(data));
    .as-console-wrapper {
      max-height: 100% !important;
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 2013-02-07
      • 2019-11-01
      相关资源
      最近更新 更多