迭代数组的对象并计算平均值

时间:2015-12-17 13:41:33

标签: javascript

我有以下数据结构,需要获取竞争对象中项目的每个列的平均值。然后我需要把它变成一个数组数组。第一个值需要是平均值(舍入),第二个值需要是从0开始的递增值。

output = [[6, 0], [4, 1], [3, 2], [3, 3], [6, 4]]; 

示例结构:

input = {
  categories: [
    "Cat 1",
    "Cat 2",
    "Cat 3",
    "Cat 4",
    "Cat 5"
  ],
  contenders: {
    item1:       [5, 3, 4, 4, 6],
    item2:       [6, 10, 4, 4, 6],
    item3:       [6, 3, 4, 9, 6],
    item4:       [8, 3, 5, 4, 6],
    item5:       [9, 3, 4, 4, 6],
    item6:       [10, 2, 7, 4, 6],
    item7:       [4, 3, 4, 4, 6],
    item8:       [1, 5, 4, 4, 6]
  },
  misc: [0, 3, 4, 4, 6]
};

我创建了一个可以为我做平均值的函数:

function getAvg(data) {
    return data.reduce(function (p, c) {
                return p + c;
            }) / data.length;
} 

但是不能完全弄清楚如何迭代项目的值以获得我的结果。

4 个答案:

答案 0 :(得分:2)

如果我理解正确并且您想要每列的平均值,这意味着您的项目键中所有数组中的每个第一个元素都是示例,那么您需要创建每个列的数组以使用您的平均函数。但是,这样做你已经可以计算平均值了,这是一种方法:



      var input = { categories: ["Cat 1", "Cat 2", "Cat 3", "Cat 4", "Cat 5"], contenders: { item1: [5, 3, 4, 4, 6], item2: [6, 10, 4, 4, 6], item3: [6, 3, 4, 9, 6], item4: [8, 3, 5, 4, 6], item5: [9, 3, 4, 4, 6], item6: [10, 2, 7, 4, 6], item7: [4, 3, 4, 4, 6], item8: [1, 5, 4, 4, 6] }, misc: [0, 3, 4, 4, 6] }


    var output = []

    var cols = input.contenders.item1.length // amount of columns (I assume all items have same amount of columns)

    for(var i=0;i<cols;i++){
      output[i] = [0,i] // start output with sum of 0 and index i
    }

    for(var key in input.contenders){
      var arr = input.contenders[key]
      for(var k = 0;k<cols;k++){
        output[k][0]+=arr[k] // add the value of the array in the kth position to the kth output sum
      }
    }

    for(var i=0;i<cols;i++){
      output[i][0] = Math.round(output[i][0]/Object.keys(input.contenders).length) // now just divide by the amount of keys
    }

    console.log(output)
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用Object.keys的组合来获取contenders中的每个项目,然后使用map从结果中创建新数组:

function getAvg(data) {
  return data.reduce(function (p, c) {
    return p + c;
  }) / data.length;
} 
var input = {
  categories: [
    "Cat 1",
    "Cat 2",
    "Cat 3",
    "Cat 4",
    "Cat 5"
  ],
  contenders: {
    item1:       [5, 3, 4, 4, 6],
    item2:       [6, 10, 4, 4, 6],
    item3:       [6, 3, 4, 9, 6],
    item4:       [8, 3, 5, 4, 6],
    item5:       [9, 3, 4, 4, 6],
    item6:       [10, 2, 7, 4, 6],
    item7:       [4, 3, 4, 4, 6],
    item8:       [1, 5, 4, 4, 6]
  },
  misc: [0, 3, 4, 4, 6]
};
console.log(Object.keys(input.contenders).map(function(key, index) {
  return [getAvg(input.contenders[key]), index];
}));

答案 2 :(得分:0)

您可以使用Object.keys获取属性,然后使用Array.prototype.forEach对值进行交互。

&#13;
&#13;
var input = { categories: ["Cat 1", "Cat 2", "Cat 3", "Cat 4", "Cat 5"], contenders: { item1: [5, 3, 4, 4, 6], item2: [6, 10, 4, 4, 6], item3: [6, 3, 4, 9, 6], item4: [8, 3, 5, 4, 6], item5: [9, 3, 4, 4, 6], item6: [10, 2, 7, 4, 6], item7: [4, 3, 4, 4, 6], item8: [1, 5, 4, 4, 6] }, misc: [0, 3, 4, 4, 6] },
    output = [],
    sum = [], count = [];

Object.keys(input.contenders).forEach(function (k) {
    input.contenders[k].forEach(function (a, i) {
        sum[i] = (sum[i] || 0) + a;
        count[i] = (count[i] || 0) + 1;
    });
});
output = sum.map(function (a, i) {
    return [Math.round(sum[i] / count[i]), i]
});

document.write('<pre>' + JSON.stringify(output, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您应完全按照其他答案中的建议使用Object.keys。但是,为了完整性或不兼容的情况,兼容性会给您带来麻烦: 你可以构建一个字符串来引用你正在寻找的数组,就像这样

for (var i=1;i<9;i++) { doSomething(input.contenders["item"+i]); }

这需要您提前知道项目数量及其名称。