let students = [
{
name: 'John',
subjects: ['maths', 'english', 'cad'],
teacher: {maths: 'Harry', english: 'Joan', cad: 'Paul'},
results: {maths: 90, english: 75, cad: 87},
},
{
name: 'Emily',
subjects: ['science', 'english', 'art'],
teacher: {science: 'Iris', english: 'Joan', art: 'Simon'},
results: {science: 93, english: 80, art: 95},
},
{
name: 'Adam',
subjects: ['science', 'maths', 'art'],
teacher: {science: 'Iris', maths: 'Harry', art: 'Simon'},
results: {science: 84, maths: 97, art: 95},
},
{
name: 'Fran',
subjects: ['science', 'english', 'art'],
teacher: {science: 'Iris', english: 'Joan', art: 'Simon'},
results: {science: 67, english: 87, art: 95},
}
];
const topMaths = students.filter((student) => student.results >= 90);
console.log(topMaths);
I’m new enough to this and couldn’t find the answer online.
Why won’t the students with over 90 come out in the console.
It shows empty [].
2
Answers
Figured it out :D
The issue with your code is in the filtering condition within the filter function. You are trying to filter students based on the results object, but the comparison student.results >= 90 is incorrect because student.results is an object, not a single value.
If you want to filter students who scored 90 or more in the ‘maths’ subject, you should access the ‘maths’ property within the results object for each student and then compare it to 90. You just need to change this line and the code should work.
const topMaths = students.filter((student) => student.results.maths >= 90); console.log(topMaths);