获取每个任务的平均费率

Get the average of rates per assignment

我有以下数据:

{
  "students": [
    { "id": 1, "name": "Barbara" },
    { "id": 2, "name": "Grace" },
    { "id": 3, "name": "John" },
],
"assignments": [   
{
    "id": 1,
    "name_id": 1,
    "assignment": "Mathematics",
    "assignment_difficulty": 3,
    "assignment_fun": 4
  },
  {
    "id": 2,
    "name_id": 1,
    "assignment": "Science",
    "assignment_difficulty": 3,
    "assignment_fun": 3
  },
  {
    "id": 3,
    "name_id": 1,
    "assignment": "German",
    "assignment_difficulty": 1,
    "assignment_fun": 3
  },
  {
    "id": 4,
    "name_id": 1,
    "assignment": "French",
    "assignment_difficulty": 2,
    "assignment_fun": 3
  },
  {
    "id": 5,
    "name_id": 1,
    "assignment": "Sports",
    "assignment_difficulty": 5,
    "assignment_fun": 4
  }
{
    "id": 6,
    "name_id": 2,
    "assignment": "Science",
    "assignment_difficulty": 4,
    "assignment_fun": 3
  },
  {
    "id": 7,
    "name_id": 2,
    "assignment": "Biology",
    "assignment_difficulty": 2,
    "assignment_fun": 5
  },
  {
    "id": 8,
    "name_id": 2,
    "assignment": "English",
    "assignment_difficulty": 1,
    "assignment_fun": 4
  },
  {
    "id": 9,
    "name_id": 2,
    "assignment": "French",
    "assignment_difficulty": 3,
    "assignment_fun": 4
  },
  {
    "id": 10,
    "name_id": 2,
    "assignment": "Sports",
    "assignment_difficulty": 2,
    "assignment_fun": 4
  },
{
    "id": 11,
    "name_id": 3,
    "assignment": "Mathematics",
    "assignment_difficulty": 1,
    "assignment_fun": 3
  },
  {
    "id": 12,
    "name_id": 3,
    "assignment": "Biology",
    "assignment_difficulty": 3,
    "assignment_fun": 4
  },
  {
    "id": 13,
    "name_id": 3,
    "assignment": "English",
    "assignment_difficulty": 4,
    "assignment_fun": 2
  },
  {
    "id": 14,
    "name_id": 3,
    "assignment": "German",
    "assignment_difficulty": 3,
    "assignment_fun": 4
  },
  {
    "id": 15,
    "name_id": 3,
    "assignment": "Sports",
    "assignment_difficulty": 2,
    "assignment_fun": 5
  }
]

现在我需要计算每项作业的平均难度和趣味率。如您所见,并非每个学生都有相同的作业。

启动 blanco 时哪种方法最好。 .map()?或者更好的 for (var i = 0; i < assignments.length; i++){//do something} 方法?

我认为首先迭代学生的作业并在该迭代中迭代比率是明智的。

或者更明智的做法是首先对人员进行迭代,然后在该迭代中对分配及其对那个人的费率进行迭代?

您可能会做一些 reduce 魔术,但我发现这对于其他遇到您的代码的人来说更容易阅读和理解:

const grouped = {} // initialize this

// Gather assignment data for calculations
data.assignments.forEach(({ assignment, assignment_fun, assignment_difficulty }) => {
  if (grouped[assignment]) {
    grouped[assignment].assignment_fun = grouped[assignment].assignment_fun + assignment_fun
    grouped[assignment].assignment_difficulty = grouped[assignment].assignment_difficulty + assignment_difficulty
    grouped[assignment].count = grouped[assignment].count + 1
  } else
    grouped[assignment.assignment] = {
      tFun: assignment.assignment_fun,
      tDifficulty: assignment.assignment_difficulty,
      count: 1
    }
}}

// finally calculate results per assignment
Object.keys(grouped).forEach((assignment) => {
  const count = grouped[assignment].count;
  console.log(`${assignment} Average Difficulty: ${grouped[assignment].tDifficulty/count}`)
  console.log(`${assignment} Average Fun: ${grouped[assignment].tFun/count}`)
})