আমাদের একটি অ্যারে আছে যেটিতে বেশ কিছু student অবজেক্ট আছে, প্রতিটি student অবজেক্টে grades নামে একটি অ্যারে প্রপার্টি রয়েছে:
const arr = [
{
name: "Student 1",
grades: [ 65, 61, 67, 70 ]
},
{
name: "Student 2",
grades: [ 50, 51, 53, 90 ]
},
{
name: "Student 3",
grades: [ 0, 20, 40, 60 ]
}
];
আমাদের একটি ফাংশন তৈরি করতে হবে যা student অ্যারেতে লুপ করে যেকোনো student অবজেক্টের grades অ্যারেতে সর্বোচ্চ গ্রেড আছে সেটি খুঁজে বের করবে।
উদাহরণ
কোডটি হবে:
const arr = [
{
name: "Student 1",
grades: [ 65, 61, 67, 70 ]
},
{
name: "Student 2",
grades: [ 50, 51, 53, 90 ]
},
{
name: "Student 3",
grades: [ 0, 20, 40, 60 ]
}
];
const highestGrades = arr.map((stud, ind) => {
return {
name: stud.name,
highestGrade: Math.max.apply(Math, stud.grades) // get a student's
highest grade
};
});
const bestStudent = highestGrades.sort((a, b) => {
return b.highestGrade - a.highestGrade;
})[0];
console.log(bestStudent.name + " has the highest score of " + bestStudent.highestGrade);
আউটপুট
কনসোলে আউটপুট হবে:
Student 2 has the highest score of 90