हमारे पास एक ऐरे है जिसमें कई ऑब्जेक्ट्स हैं जिनका नाम 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) // एक student का 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