计算对象中标记的平均值

Calculate average of marks in an object

我一直在为学习目的而做的一项任务而苦苦挣扎。我有一个物体,里面有几个学生,我想计算史蒂文的平均分。

示例:

const students = [
{
  name: 'John',
  surname: 'Johnson',
  faculty: 'Faculty of Science',
  modules: [
    {
      title: 'Operating systems',
      marks: [10, 10, 9]
    },
    {
      title: 'Algorythms',
      marks: [8, 10, 8]
    },
    {
      title: 'Statistics',
      marks: [9, 7, 8]
    },
  ]
},
{
  name: 'Steven',
  surname: 'Stevenson',
  faculty: 'Faculty of Science',
  modules: [
    {
      title: 'Operating systems',
      marks: [7, 6, 9]
      },
      {
      title: 'Algorythms',
      marks: [7, 8, 9]
      },  
      {  
      title: 'Statistics',
      marks: [6, 8, 10]
      },
  ]
},
];

使用过滤方法,我只能从给定对象中过滤出 Steven。

const studentSteven = students.filter(student => student.name === 'Steven');

如何计算他所有模块的平均分?我想使用数组方法来实现这一点。任何提示将不胜感激。

  1. flatMap 创建一个标记数组。

  2. reduce 在该数组上创建总体平均值。

  3. Return 更新对象。

const students=[{name:"John",surname:"Johnson",faculty:"Faculty of Science",modules:[{title:"Operating systems",marks:[10,10,9]},{title:"Algorythms",marks:[8,10,8]},{title:"Statistics",marks:[9,7,8]}]},{name:"Steven",surname:"Stevenson",faculty:"Faculty of Science",modules:[{title:"Operating systems",marks:[7,6,9]},{title:"Algorythms",marks:[7,8,9]},{title:"Statistics",marks:[6,8,10]}]}];

const out = students.map(obj => {

  // Destructure the modules property from the
  // rest of the object properties
  const { modules, ...rest } = obj;

  // Return each marks array, and then flatten them
  const marks = modules.flatMap(obj => obj.marks);

  // `reduce` over the marks array to create a overall
  // sum, divide it by the number of marks,
  // and round up the returned floating-point number
  const average = Math.round(marks.reduce((acc, c) => {
    return acc + c;
  }, 0) / marks.length);

  // Return a new updated object
  return { ...rest, average, modules };

});

console.log(out);

其他文档


const students = [
{
  name: 'John',
  surname: 'Johnson',
  faculty: 'Faculty of Science',
  modules: [
    {
      title: 'Operating systems',
      marks: [10, 10, 9]
    },
    {
      title: 'Algorythms',
      marks: [8, 10, 8]
    },
    {
      title: 'Statistics',
      marks: [9, 7, 8]
    },
  ]
},
{
  name: 'Steven',
  surname: 'Stevenson',
  faculty: 'Faculty of Science',
  modules: [
      {
      title: 'Operating systems',
      marks: [7, 6, 9]
      },
      {
      title: 'Algorythms',
      marks: [7, 8, 9]
      },  
      {  
      title: 'Statistics',
      marks: [6, 8, 10]
      },
  ]
},
];

var studentSteven = students.filter(student => student.name === 'Steven');
studentSteven = studentSteven[0]
let g = 0;
let sum = 0;
for(i = 0;i < studentSteven.modules.length;i++){
sum += studentSteven.modules[i].marks.reduce((s,t)=>{
  g++
  return s + t
})
g++
}
avg = sum/g


您可以 map 对学生数组和每个学生再次 map 对模块数组并添加一个名为 averageMarks 的 属性 到每个模块对象。

const students = [
  {
    name: "John",
    surname: "Johnson",
    faculty: "Faculty of Science",
    modules: [
      { title: "Operating systems", marks: [10, 10, 9] },
      { title: "Algorythms", marks: [8, 10, 8] },
      { title: "Statistics", marks: [9, 7, 8] },
    ],
  },
  {
    name: "Steven",
    surname: "Stevenson",
    faculty: "Faculty of Science",
    modules: [
      { title: "Operating systems", marks: [7, 6, 9] },
      { title: "Algorythms", marks: [7, 8, 9] },
      { title: "Statistics", marks: [6, 8, 10] },
    ],
  },
];

const updatedStudents = students.map((st) => ({
  ...st,
  modules: st.modules.map((mod) => ({
    ...mod,
    averageMarks: mod.marks.reduce((sum, m) => sum + m) / mod.marks.length,
  })),
}));

console.log(updatedStudents);

如果您还需要计算所有模块的平均值,那么您可以将上述结果与另一个 map 链接起来,并为每个学生添加一个 average 属性。

const students = [
  {
    name: "John",
    surname: "Johnson",
    faculty: "Faculty of Science",
    modules: [
      { title: "Operating systems", marks: [10, 10, 9] },
      { title: "Algorythms", marks: [8, 10, 8] },
      { title: "Statistics", marks: [9, 7, 8] },
    ],
  },
  {
    name: "Steven",
    surname: "Stevenson",
    faculty: "Faculty of Science",
    modules: [
      { title: "Operating systems", marks: [7, 6, 9] },
      { title: "Algorythms", marks: [7, 8, 9] },
      { title: "Statistics", marks: [6, 8, 10] },
    ],
  },
];

const updatedStudents = students
  .map((st) => ({
    ...st,
    modules: st.modules.map((mod) => ({
      ...mod,
      averageMarks: mod.marks.reduce((sum, m) => sum + m) / mod.marks.length,
    })),
  }))
  .map((st) => ({
    ...st,
    average:
      st.modules.reduce((sum, { averageMarks }) => sum + averageMarks, 0) /
      st.modules.length,
  }));

console.log(updatedStudents);