2017-04-24 132 views
0

我有一個數組,其中包含名爲學生的幾個對象,每個對象的學生有幾個屬性,其中之一是一個名爲成績的數組。如何在對象數組中找到保存最高值的對象?

我需要創建一個循環遍歷學生數組並查找哪個學生對象在其成績數組內部具有最高成績的函數。

目前我能夠找到最高分,但無法理解如何追溯到它屬於哪個學生。

這裏的功能看起來像一個片段:

function bestStudent() { 
    var bestGrade = 0; 
    var student; 
    for(i=0; i < studentArr.length; i++) { 
     var student = studentArr[i]; 
     grades = student.grades; 
     for(g = 0; g <grades.length; g++){ 
      if(grades[g] > bestGrade) { 
       bestGrade = grades[g];  
      } 
     } 

    }  
} 
+2

你爲什麼不它是在同一時間只儲存哪個學生在更新bestGrade變量? –

+4

看起來像功課:3 – Mazz

+0

當你找到最好的年級,'學生'仍然會提及你正在考試的學生。 – Shilly

回答

1

的總體思路是:您可以將您的學生與他們的成績排列第一映射到學生的陣列及其檔次最高爲了方便使用並避免多次查找最大分數計算,然後查找最大的學生最高分數。

只是一個例子:

var students = [ 
 
    { 
 
    name: "Student 1", 
 
    grades: [ 65, 61, 67, 70 ] 
 
    }, 
 
    { 
 
    name: "Student 2", 
 
    grades: [ 50, 51, 53, 90 ] 
 
    }, 
 
    { 
 
    name: "Student 3", 
 
    grades: [ 0, 20, 40, 60 ] 
 
    } 
 
]; 
 
    
 
var highestGrades = students.map(function(stud, ind) { 
 
    // return a student's name and his highest grade (1) 
 
    return { 
 
    name: stud.name, 
 
    highestGrade: Math.max.apply(Math, stud.grades) // get a student's highest grade 
 
    }; 
 

 
    // or return index and use it to access original value: (2) 
 
    // return { 
 
    // index: ind, 
 
    // highestGrade: Math.max.apply(Math, stud.grades) 
 
    // }; 
 

 
    // or return the whole student: (3) 
 
    // return { 
 
    // student: stud, 
 
    // highestGrade: Math.max.apply(Math, stud.grades) 
 
    // }; 
 

 
    // or just add 'highestGrade' property to object without modifying 
 
    // if it's ok for you to have intermediate properties in your object: (4) 
 
    // stud.highestGrade = Math.max.apply(Math, stud.grades); 
 
    // return stud; 
 
}); 
 

 
// this can be done in O(n), not in O(N * logN) if required: 
 
var bestStudent = highestGrades.sort(function(a, b) { 
 
    return b.highestGrade - a.highestGrade; 
 
})[0]; // sort by highest grade desc and return the first (the best) one 
 

 
// Here we have bestStudent with his name according to map function: 
 
console.log(bestStudent.name + " has the highest score of " + bestStudent.highestGrade); // (1) 
 
// console.log(students[bestStudent.index].name + " has the highest score of " + bestStudent.highestGrade); // (2) 
 
// console.log(bestStudent.student.name + " has the highest score of " + bestStudent.highestGrade); // (3) 
 
// console.log(bestStudent.name + " has the highest score of " + bestStudent.highestGrade); // (4)

,使其返回整個學生作爲結果,或它的索引,或者其特定的屬性可以重寫該代碼。如果您的對象具有額外的中間屬性,那麼也可以將highestGrade屬性添加到原始對象。這取決於你,這個想法並沒有改變:)

這段代碼很長,但它是可讀的,使算法清晰的想法,這是非常重要的,因爲你是一個初學者。
如果你和你的團隊是更短但更復雜代碼的粉絲,那麼你可以輕鬆地重寫它。
只是這樣的事情:

var students = [ 
 
    { 
 
    name: "Student 1", 
 
    grades: [ 65, 61, 67, 70 ] 
 
    }, 
 
    { 
 
    name: "Student 2", 
 
    grades: [ 50, 51, 53, 90 ] 
 
    }, 
 
    { 
 
    name: "Student 3", 
 
    grades: [ 0, 20, 40, 60 ] 
 
    } 
 
]; 
 

 
var bestStudent = students.map(function(stud) { 
 
    stud.highestGrade = Math.max.apply(Math, stud.grades); 
 
    return stud; 
 
}).sort(function(a, b) { 
 
    return b.highestGrade - a.highestGrade; 
 
})[0]; 
 

 
console.log(bestStudent);

0

通過使用相同的功能,可以存儲位置或適當的領域

function bestStudent() { 
     var bestStudent = {}; 
     bestStudent.bestGrade = 0; 
     var student; 
     for(i=0; i < studentArr.length; i++) { 
      var student = studentArr[i]; 
      grades = student.grades; 
      for(g = 0; g <grades.length; g++){ 
      if(grades[g] > bestStudent.bestGrade) { 
       bestStudent.bestGrade = grades[g]; 
       bestStudent.name = studentArr[i].name;  
      } 
      } 

     } 

     return bestStudent; 
    } 
0

\t var students = [ 
 
\t \t { 
 
\t \t \t name: "Student 1", 
 
\t \t \t grades: [ 
 
\t \t \t \t 90, 98, 80 
 
\t \t \t ], 
 
\t \t \t getMyHighest: function(){ 
 
\t \t \t \t return Math.max.apply(Math, this.grades); 
 
\t \t \t } 
 
\t \t }, 
 
\t \t { 
 
\t \t \t name: "Student 2", 
 
\t \t \t grades: [ 
 
\t \t \t \t 75, 85, 79 
 
\t \t \t ], 
 
\t \t \t getMyHighest: function(){ 
 
\t \t \t \t return Math.max.apply(Math, this.grades); 
 
\t \t \t } 
 
\t \t } 
 
\t \t , 
 
\t \t { 
 
\t \t \t name: "Student 3", 
 
\t \t \t grades: [ 
 
\t \t \t \t 75, 85, 99 
 
\t \t \t ], 
 
\t \t \t getMyHighest: function(){ 
 
\t \t \t \t return Math.max.apply(Math, this.grades); 
 
\t \t \t } 
 
\t \t } 
 
\t ]; 
 
\t 
 
\t var student = students.sort(function(f, l) { 
 
\t return l.getMyHighest() - f.getMyHighest(); 
 
\t })[0]; 
 

 
console.log(student);

0

使用lodash.js做事情容易:)

var students=[{Grades:[1,2,3]},{Grades:[5,4,3]},{Grades:[7,77,4]}]; 


var studentWithBestGrade=_.map(students,function(student,position){ 

       return [position,_.max(student.Grades)]; 

}); 


console.log(studentWithBestGrade) //[[0,3],[1,5],[2,77]] 

發現JSFIDDLE

0

我的解決辦法是用減少JS

只有一次學生:

var bestStudent = students.reduce(function(a, student){ 
    return Math.max.apply(null, student.grades) > 
      Math.max.apply(null, a.grades) ? 
      student : a;}, students[0]); 

console.log('Best student is: ' + bestStudent.name + ' with score: ' 
      + Math.max.apply(null, bestStudent.grades)); 

例子: https://jsfiddle.net/ahx8jh5g/

在情況下,你想獲得具有最佳年級所有學生,讓我們將其存儲在一個數組:

var bestStudents = students.reduce(function(a, student){ 
    var maxGradeStu = Math.max.apply(null, student.grades), 
     maxGradeCur = a.length > 0 ? Math.max.apply(null, a[0].grades) : 0; 

     if (maxGradeStu === maxGradeCur) { 
      return a.concat(student); 
     } 
     return maxGradeStu > maxGradeCur ? [student] : a; 
}, []) 
bestStudents.forEach(bestStudent => console.log('Best student is: ' bestStudent.name + ' with score: ' 
      + Math.max.apply(null, bestStudent.grades))); 

更多的細節,你可以看到降低的位置:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

0

你可以使用一個數組對於成績最好的學生,如果你有不止一個。

然後檢查maxGrade是否與bestGrade相同,然後將實際學生推送到結果集並計數for循環。

如果maxGrade大於bestGrade,則存儲將實際學生放在新數組中的值。

function bestStudents(studentArr) { 
 
    var bestGrade = 0, 
 
     bestStudents = [], 
 
     i, 
 
     maxGrade; 
 

 
    for (i = 0; i < studentArr.length; i++) { 
 
     maxGrade = Math.max.apply(null, studentArr[i].grades); 
 
     if (maxGrade === bestGrade) { 
 
      bestStudents.push(studentArr[i]); 
 
      continue; 
 
     } 
 
     if (maxGrade > bestGrade) { 
 
      bestGrade = maxGrade; 
 
      bestStudents = [studentArr[i]]; 
 
     } 
 
    } 
 
    return bestStudents; 
 
} 
 

 
var students = [{ name: "Student 1", grades: [90, 98, 99] }, { name: "Student 2", grades: [75, 85, 79] }, { name: "Student 3", grades: [75, 85, 99] }]; 
 

 
console.log(bestStudents(students)); 
 
.as-console-wrapper { max-height: 100% !important; top: 0; }

相關問題