我有一个数组,其中包含几个名为student的对象,每个对象学生都有几个属性,其中一个是名为grade的数组。
我需要创建一个循环遍历学生数组的函数,并找到哪个学生对象在其成绩数组中具有最高等级。
目前我能够找到最高分,但无法理解如何追溯到哪个学生所属。
以下是该函数的代码示例:
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];
}
}
}
}
答案 0 :(得分: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);
答案 1 :(得分: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;
}
答案 2 :(得分:0)
var students = [
{
name: "Student 1",
grades: [
90, 98, 80
],
getMyHighest: function(){
return Math.max.apply( Math, this.grades );
}
},
{
name: "Student 2",
grades: [
75, 85, 79
],
getMyHighest: function(){
return Math.max.apply( Math, this.grades );
}
}
,
{
name: "Student 3",
grades: [
75, 85, 99
],
getMyHighest: function(){
return Math.max.apply( Math, this.grades );
}
}
];
var student = students.sort(function(f, l) {
return l.getMyHighest() - f.getMyHighest();
})[0];
console.log(student);
&#13;
答案 3 :(得分: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
答案 4 :(得分:0)
我的解决方案是在JS中使用reduce
只有一个学生:
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
答案 5 :(得分: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));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;