Chúng tôi có một mảng chứa một số đối tượng có tên là sinh viên, mỗi sinh viên đối tượng có một số thuộc tính, một trong số đó là một mảng có tên là các lớp -
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 ] } ];
Chúng ta cần tạo một chức năng lặp qua mảng của học sinh và tìm thấy đối tượng học sinh nào có lớp cao nhất bên trong mảng điểm của nó.
Thí dụ
Mã cho điều này sẽ là -
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];
Đầu ra
Và đầu ra trong bảng điều khiển sẽ là -
Student 2 has the highest score of 90
Cập nhật vào ngày 20 tháng 11 năm 2020 13:50:31
- Câu hỏi và câu trả lời liên quan
- Trả lại giá trị cao nhất từ một mảng trong JavaScript
- Chuyển đổi mảng đối tượng thành một đối tượng của các đối tượng trong JavaScript
- Các đối tượng nhóm bên trong mảng lồng nhau javascript
- Phân tích mảng của các đối tượng bên trong một đối tượng sử dụng bản đồ hoặc foreach bằng cách sử dụng javascript?
- Tìm n giá trị cao nhất trong một đối tượng javascript
- Chuyển đổi mảng đối tượng thành một đối tượng trong JavaScript
- Làm thế nào để tìm giá trị tối đa của một mảng trong JavaScript?
- Làm thế nào để tìm giá trị tối thiểu của một mảng trong JavaScript?
- Làm thế nào để chuyển đổi đối tượng của các đối tượng thành đối tượng của mảng các đối tượng với JavaScript?
- Chia một đối tượng thành một mảng các đối tượng trong JavaScript
- Tìm giá trị gần nhất của một mảng trong JavaScript
- Làm thế nào để chèn một mục vào một mảng bên trong một đối tượng trong MongoDB?
- Chuyển đổi mảng các đối tượng thành một đối tượng của mảng trong javascript
- Chèn giá trị ở giữa mỗi giá trị bên trong mảng javascript
- Làm thế nào để chỉnh sửa các giá trị của một đối tượng bên trong một mảng trong một lớp - javascript?
Tôi có một mảng chứa một số đối tượng có tên là Sinh viên, mỗi sinh viên đối tượng có một số thuộc tính, một trong số đó là một mảng có tên là các lớp.
Tôi cần tạo một chức năng lặp qua mảng của học sinh và tìm thấy đối tượng học sinh nào có lớp cao nhất bên trong mảng điểm của nó.
Hiện tại tôi có thể tìm thấy điểm số cao nhất, nhưng không thể hiểu làm thế nào để truy tìm lại học sinh mà nó thuộc về.
Dưới đây là một đoạn về cách chức năng trông như thế nào:
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 bestGrade] {
bestGrade = grades[g];
}
}
}
}
Hỏi ngày 24 tháng 4 năm 2017 lúc 14:16Apr 24, 2017 at 14:16
6
Ý tưởng chung là như sau: Trước tiên bạn có thể ánh xạ mảng học sinh của mình với điểm số của họ đến một loạt học sinh và lớp cao nhất của họ để làm cho nó thuận tiện để làm việc và tránh nhiều tính toán cấp độ tìm kiếm, sau đó tìm Lớn nhất trong lớp cao nhất của học sinh.
Chỉ là một ví dụ:
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]
Bạn có thể viết lại mã này để nó trả về toàn bộ học sinh là kết quả hoặc chỉ số của nó hoặc các thuộc tính cụ thể của nó. Bạn cũng có thể chỉ cần thêm thuộc tính
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];3 vào đối tượng gốc nếu các đối tượng của bạn có một thuộc tính trung gian bổ sung. Đó là tùy thuộc vào bạn, ý tưởng không thay đổi :]
Mã này khá dài, nhưng nó có thể đọc được và làm cho ý tưởng về thuật toán rõ ràng, nó rất quan trọng vì bạn là người mới bắt đầu. Nếu bạn và nhóm của bạn là người hâm mộ của mã ngắn hơn nhưng phức tạp hơn, thì bạn có thể dễ dàng viết lại nó. Chỉ là một cái gì đó như thế này:
If you and your team are fans of shorter but more complex code, then you can easily rewrite it.
Just something like this:
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];
Đã trả lời ngày 24 tháng 4 năm 2017 lúc 14:27Apr 24, 2017 at 14:27
Bằng cách sử dụng cùng một chức năng, bạn có thể lưu trữ vị trí hoặc trường thích hợp
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 bestStudent.bestGrade] {
bestStudent.bestGrade = grades[g];
bestStudent.name = studentArr[i].name;
}
}
}
return bestStudent;
}
Đã trả lời ngày 24 tháng 4 năm 2017 lúc 14:28Apr 24, 2017 at 14:28
MilsmilsMils
1.4592 huy hiệu vàng19 Huy hiệu bạc42 Huy hiệu đồng2 gold badges19 silver badges42 bronze badges
Sử dụng lodash.js để làm cho mọi thứ dễ dàng :]
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]]
Tìm nó jsfiddle
Đã trả lời ngày 24 tháng 4 năm 2017 lúc 14:32Apr 24, 2017 at 14:32
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];
Đã trả lời ngày 24 tháng 4 năm 2017 lúc 14:28Apr 24, 2017 at 14:28
Milsmilsgenichm
1.4592 huy hiệu vàng19 Huy hiệu bạc42 Huy hiệu đồng7 silver badges18 bronze badges
Sử dụng lodash.js để làm cho mọi thứ dễ dàng :]
Tìm nó jsfiddle
Đã trả lời ngày 24 tháng 4 năm 2017 lúc 14:32
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]];
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];0
GenichmgenichmApr 24, 2017 at 14:45
5257 Huy hiệu bạc18 Huy hiệu ĐồngNina Scholz
Bạn có thể sử dụng một mảng cho các học sinh có điểm tốt nhất, nếu bạn có nhiều hơn một.24 gold badges326 silver badges366 bronze badges
Sau đó kiểm tra xem
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];4 có giống với
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];5 không, sau đó đẩy học sinh thực tế vào tập kết quả và đếm vòng lặp cho vòng lặp.
Nếu
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];4 lớn hơn
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];5, hãy lưu trữ giá trị cho một học sinh thực tế vào một mảng mới.
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];1
Đã trả lời ngày 24 tháng 4 năm 2017 lúc 14:45
Nina Scholznina Scholz
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];2
361K24 Huy hiệu vàng326 Huy hiệu bạc366 Huy hiệu Đồng
Giải pháp của tôi sẽ được sử dụng giảm trong JSApr 24, 2017 at 14:35
Chỉ nhận được một học sinh:taile
Ví dụ: //jsfiddle.net/ahx8jh5g/16 silver badges28 bronze badges