Home > OS >  Get the average of rates per assignment
Get the average of rates per assignment

Time:04-15

I have the following data:

{
  "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
  }
]

Now I need to calculate the average difficulty and fun rates per assignment. As you can see, not every student has the same assignments.

What approach is the best when starting blanco. .map()? Or better for (var i = 0; i < assignments.length; i ){//do something} methode?

I assume it is wise to first iterate over the assignments by students and iterate within that iterate over the rates.

Or is wiser to iterate over the persons first and within that iteration over the assignments and its rates for that person?

CodePudding user response:

You could probably do some reduce magic but this what I find easier to read and understand for others coming across your code:

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}`)
})
  • Related