计算每个对象数组的平均得分

时间:2020-02-10 13:02:31

标签: javascript arrays json object

我正在努力寻找正确的方法来计算对象中每个数组的平均得分。 有199个数组,我需要计算每个数组的平均得分并返回输出

const grouped = {}
const score = {}

obj.forEach(function (a) {
  grouped[a.sentence_pair_id] = grouped[a.sentence_pair_id] || []
  score[a.sentence_pair_id] = score[a.sentence_pair_id] || []

  grouped[a.sentence_pair_id].push({ sentence_pair_id: a.sentence_pair_id, score: a.score, evaluator_id: a.evaluator_id })


  score[a.sentence_pair_id].push({ score: a.score })

})

console.log(JSON.stringify(score, 0, 2))

这是我到目前为止所拥有的。 有人可以提出建议以解决这个问题吗? 在第二个练习中,我必须返回一个JSON,其中包含平均分数以及最小和最大数字 谢谢

{
  "BG_SE_1": [
    {
      "score": "45"
    },
    {
      "score": "52"
    },
    {
      "score": "46"
    },
    {
      "score": "49"
    },
    {
      "score": "44"
    }
  ],
  "BG_SE_2": [
    {
      "score": "17"
    },
    {
      "score": "9"
    },
    {
      "score": "13"
    },
    {
      "score": "15"
    },
    {
      "score": "12"
    }
  ],
  "BG_SE_4": [
    {
      "score": "92"
    },
    {
      "score": "94"
    },
    {
      "score": "90"
    },
    {
      "score": "96"
    },
    {
      "score": "94"
    }
  ],
  "BG_SE_5": [
    {
      "score": "11"
    },
    {
      "score": "14"
    },
    {
      "score": "17"
    },
    {
      "score": "15"
    },
    {
      "score": "13"
    }
  ],
  "BG_SE_6": [
    {
      "score": "63"
    },
    {
      "score": "65"
    },
    {
      "score": "64"
    },
    {
      "score": "66"
    },
    {
      "score": "69"
    }
  ],

3 个答案:

答案 0 :(得分:1)

您可以在数组上使用reduce方法来计算总和,然后求平均值。类似地,相同的逻辑可用于最小和最大。像这样

const input = {
  "BG_SE_1": [
    {
      "score": "45"
    },
    {
      "score": "52"
    },
    {
      "score": "46"
    },
    {
      "score": "49"
    },
    {
      "score": "44"
    }
  ],
  "BG_SE_2": [
    {
      "score": "17"
    },
    {
      "score": "9"
    },
    {
      "score": "13"
    },
    {
      "score": "15"
    },
    {
      "score": "12"
    }
  ],
  "BG_SE_4": [
    {
      "score": "92"
    },
    {
      "score": "94"
    },
    {
      "score": "90"
    },
    {
      "score": "96"
    },
    {
      "score": "94"
    }
  ],
  "BG_SE_5": [
    {
      "score": "11"
    },
    {
      "score": "14"
    },
    {
      "score": "17"
    },
    {
      "score": "15"
    },
    {
      "score": "13"
    }
  ],
  "BG_SE_6": [
    {
      "score": "63"
    },
    {
      "score": "65"
    },
    {
      "score": "64"
    },
    {
      "score": "66"
    },
    {
      "score": "69"
    }
  ]
};

Object.keys(input).forEach(item => {
  const sum = input[item].map(i => i.score).reduce((accumulator, currentValue) => parseInt(accumulator, 10) + parseInt(currentValue, 10));
  const avg = sum / input[item].length;
  console.log(item, avg);
});

答案 1 :(得分:0)

obj设置为对象数组:

let obj = [ 
  {"sentence_pair_id":"BG_SE_1","evaluator_id":"BBC_Bulgarian_01","score":"45","human_translation":"Защо американският флаг се развява?","machine_translation":"Как се развява това американско знаме?","original":"How did that US flag wave?"},
  {"sentence_pair_id":"BG_SE_1","evaluator_id":"DW_Bulgarian_01","score":"52","human_translation":"Защо американският флаг се развява?","machine_translation":"Как се развява това американско знаме?","original":"How did that US flag wave?"},
] 

let grouped_scores = obj.reduce(function(memo, item) {
  if (!memo[item.sentence_pair_id]) {
    memo[item.sentence_pair_id] = {score: 0, count: 0}
  }

  memo[item.sentence_pair_id].score += Number(item.score)
  memo[item.sentence_pair_id].count += 1
  return memo
}, {})

let grouped_average_scores = Object.keys(grouped_scores).reduce(function(memo, key) {
  memo[key] = grouped_scores[key].score / grouped_scores[key].count
  return memo
}, {})

结果:

{BG_SE_1: 48.5}

答案 2 :(得分:0)

在这里,我添加了一个与您的案件有关的示例。我希望它能帮助您按数组项查找最小,最大和平均得分。

 var testObj = {
            "BG_SE_1": [
                {
                    "score": "45"
                },
                {
                    "score": "52"
                },
                {
                    "score": "46"
                },
                {
                    "score": "49"
                },
                {
                    "score": "44"
                }
            ],
            "BG_SE_2": [
                {
                    "score": "17"
                },
                {
                    "score": "9"
                },
                {
                    "score": "13"
                },
                {
                    "score": "15"
                },
                {
                    "score": "12"
                }
            ],
            "BG_SE_4": [
                {
                    "score": "92"
                },
                {
                    "score": "94"
                },
                {
                    "score": "90"
                },
                {
                    "score": "96"
                },
                {
                    "score": "94"
                }
            ],
            "BG_SE_5": [
                {
                    "score": "11"
                },
                {
                    "score": "14"
                },
                {
                    "score": "17"
                },
                {
                    "score": "15"
                },
                {
                    "score": "13"
                }
            ],
            "BG_SE_6": [
                {
                    "score": "63"
                },
                {
                    "score": "65"
                },
                {
                    "score": "64"
                },
                {
                    "score": "66"
                },
                {
                    "score": "69"
                }
            ]
        }

        // object with array result of min,max,avg
        var arrObj = [];       

        for (var propt in testObj) {      

            var dataLength = testObj[propt].length;
            var arrData = [];
            var total = 0;
            for (i = 0; i < dataLength; i++) {
                arrData.push(testObj[propt][i].score);
                total += testObj[propt][i].score;
            }
            Math.max.apply(Math,arrData); 
            Math.min.apply(Math, arrData);

            arrObj.push( {
                name: propt,
                MinScore: Math.min.apply(Math, arrData),
                MaxScore: Math.max.apply(Math,arrData),
                AvgScore: total/dataLength
            });

        }