Here I am not able to hide the lower mark field
let result = [];
var Avgmarks = 0,i;
let students = [{name: "Student1",sub: {math: 80,eng: 50,science: 70}},{name: "Student2",sub:{math: 40,eng: 65,science: 85}}];
students.forEach( student => {
result.push({
name: student.name,
Subject: student.sub,
marks: Object.values(student.sub).reduce((a, b) => a + b),
Average: ( Object.values(student.sub).reduce((a, b) => (a + b)) ) / ( Object.values(student.sub).length )
})
});
console.log(result)
[
{
name: ‘Student1’,
Subject: { math: 80, eng: 50, science: 70 },
marks: 200,
Average: 120
},
{
name: ‘Student2’,
Subject: { math: 40, eng: 65, science: 85 },
marks: 190,
Average: 90
}
]
>Solution :
the way you are calculating average using reduce is wrong
if you know there is going to be 3 subjects always.. you can try
{ Average: Object.values(student.sub).reduce((a, b) => (a + b))/3 }
if subjects length can vary
Average: ( Object.values(student.sub).reduce((a, b) => (a + b)) ) / ( Object.values(student.sub).length )