按 id 过滤对象并找到最高分
Filter object by id and find highest score
我试图找到正确的语法来按 id 对对象进行排序,然后找到该 id 数组的最高分。到目前为止,我已经使用条件语句和 sort() 方法对对象进行了排序,但我想通过 lesson_id 将对象压缩到最高分。我错过了什么?
到目前为止我得到的是:
34: {user: 6, lesson_id: 2, score: 50, complete: true}
35: {user: 6, lesson_id: 1, score: 50, complete: true}
36: {user: 6, lesson_id: 2, score: 50, complete: true}
38: {user: 6, lesson_id: 2, score: 100, complete: true}
39: {user: 6, lesson_id: 2, score: 100, complete: true}
40: {user: 6, lesson_id: 2, score: 100, complete: true}
但我想要这样:
0: {user: 6, lesson_id: 2, score: 100, complete: true}
1: {user: 6, lesson_id: 1, score: 50, complete: true}
这是我目前得到的结果 (Vue3.js):
mounted() {
this.quizArray = response.data;
const arrayFilter = this.quizArray;
const user_id = this.user_profile[0]["id"];
var newObj = [];
var listSort = function () {
for (var i = 0; i < arrayFilter.length; i++) {
if (arrayFilter[i].student["id"] === user_id) {
if (arrayFilter[i].score !== null) {
if (arrayFilter[i].complete === true) {
arrayFilter.sort((a, b) => a.score - b.score);
//not sure about this part ....
newObj[i] = {
user: arrayFilter[i].student["id"],
lesson_id: arrayFilter[i].quiz["lesson_id"],
score: arrayFilter[i].score,
complete: arrayFilter[i].complete
}
}
}
}
}
};
newObj = this.filteredResults;
console.log(this.filteredResults)
listSort();
// const highest = this.filteredResults.sort((a, b) => b.score - a.score)[0]
// console.log(highest) works but only for one lesson
});
},
这是原始对象:
35:
complete: true
quiz: {id: 1, lesson_id: 1, question: 'What is heat transfer?', answer: 'Its is a type of blah', op1: 'Its balahsdd', …}
score: 50
student: {user: 'Mark', name: 'Mark', occupation: 'Commissioning Technician', residence: 'Australia', active_id: false, …}
我建议为此使用 JS 方法,它会清理代码。此外,我看不到对数组进行排序的意义,如果您只需要最高值,则可以使用下面的代码段。
@EDIT 我忘记了每节课的分组,更新了下面的答案
const userId = this.user_profile[0]["id"];
// get all completed quiz elements for student and with score different than null
const quizForStudent = this.quizArray.filter(quiz => quiz.student.id === userId && quiz.complete && quiz.score !== null)
// find highest value for all lessons
// const highest = quizForStudent.reduce((prev, current) => prev.score > current.score ? prev : current)
// group elements per lesson id. this will give result in format
// {
// 2: [{complete: true, quiz: {lesson_id: 2, ...}, score: 100, student: {...}, {complete: true, quiz: {lesson_id: 2, ...}, score: 100, student: {...}, ...]
// 1: [{complete: true, quiz: {lesson_id: 1, ...}, score: 100, student: {...}]
// }
const scorePerLesson = quizForStudent.reduce((object, current) => {
// make sure array exist for given lessonId
object[current.quiz.lesson_id] = object[current.quiz.lesson_id] || []
// add array to object
object[current.quiz.lesson_id].push(current)
return object
},{})
// find highest score for each lesson
// {
// 2: {complete: true, quiz: {lesson_id: 2, ...}, score: 100, student: {...}
// 1: {complete: true, quiz: {lesson_id: 1, ...}, score: 100, student: {...}
// }
const highestScorePerLesson = Object.entries(scorePerLesson).reduce((object, [lessonId, quiz]) => ({
...object,
[lessonId]: quiz.reduce((prev, current) => prev.score > current.score ? prev : current)
}), {})
要获得给定课程(例如第 2 课)的最高分,您可以使用
highestScore[2]
在沙盒中签出:link
Array.reduce 可用于此:
const quizArray = [
{user: 6, lesson_id: 2, score: 50, complete: true},
{user: 6, lesson_id: 1, score: 50, complete: true},
{user: 6, lesson_id: 2, score: 50, complete: true},
{user: 6, lesson_id: 2, score: 100, complete: true},
{user: 6, lesson_id: 2, score: 100, complete: true},
{user: 6, lesson_id: 2, score: 100, complete: true}
];
let highScores = Object.values(quizArray
.filter(entry => entry.complete) // Only consider completed entries
.reduce((highScores, entry) => {
// Look up current high score for this lesson
let lesson = highScores[entry.lesson_id];
// If there is no score, or the new score is higher, then switch in new entry
if (!lesson || (entry.score > lesson.score)) { highScores[entry.lesson_id] = entry; }
return highScores;
}, {}));
console.log(highScores);
我试图找到正确的语法来按 id 对对象进行排序,然后找到该 id 数组的最高分。到目前为止,我已经使用条件语句和 sort() 方法对对象进行了排序,但我想通过 lesson_id 将对象压缩到最高分。我错过了什么?
到目前为止我得到的是:
34: {user: 6, lesson_id: 2, score: 50, complete: true}
35: {user: 6, lesson_id: 1, score: 50, complete: true}
36: {user: 6, lesson_id: 2, score: 50, complete: true}
38: {user: 6, lesson_id: 2, score: 100, complete: true}
39: {user: 6, lesson_id: 2, score: 100, complete: true}
40: {user: 6, lesson_id: 2, score: 100, complete: true}
但我想要这样:
0: {user: 6, lesson_id: 2, score: 100, complete: true}
1: {user: 6, lesson_id: 1, score: 50, complete: true}
这是我目前得到的结果 (Vue3.js):
mounted() {
this.quizArray = response.data;
const arrayFilter = this.quizArray;
const user_id = this.user_profile[0]["id"];
var newObj = [];
var listSort = function () {
for (var i = 0; i < arrayFilter.length; i++) {
if (arrayFilter[i].student["id"] === user_id) {
if (arrayFilter[i].score !== null) {
if (arrayFilter[i].complete === true) {
arrayFilter.sort((a, b) => a.score - b.score);
//not sure about this part ....
newObj[i] = {
user: arrayFilter[i].student["id"],
lesson_id: arrayFilter[i].quiz["lesson_id"],
score: arrayFilter[i].score,
complete: arrayFilter[i].complete
}
}
}
}
}
};
newObj = this.filteredResults;
console.log(this.filteredResults)
listSort();
// const highest = this.filteredResults.sort((a, b) => b.score - a.score)[0]
// console.log(highest) works but only for one lesson
});
},
这是原始对象:
35:
complete: true
quiz: {id: 1, lesson_id: 1, question: 'What is heat transfer?', answer: 'Its is a type of blah', op1: 'Its balahsdd', …}
score: 50
student: {user: 'Mark', name: 'Mark', occupation: 'Commissioning Technician', residence: 'Australia', active_id: false, …}
我建议为此使用 JS 方法,它会清理代码。此外,我看不到对数组进行排序的意义,如果您只需要最高值,则可以使用下面的代码段。 @EDIT 我忘记了每节课的分组,更新了下面的答案
const userId = this.user_profile[0]["id"];
// get all completed quiz elements for student and with score different than null
const quizForStudent = this.quizArray.filter(quiz => quiz.student.id === userId && quiz.complete && quiz.score !== null)
// find highest value for all lessons
// const highest = quizForStudent.reduce((prev, current) => prev.score > current.score ? prev : current)
// group elements per lesson id. this will give result in format
// {
// 2: [{complete: true, quiz: {lesson_id: 2, ...}, score: 100, student: {...}, {complete: true, quiz: {lesson_id: 2, ...}, score: 100, student: {...}, ...]
// 1: [{complete: true, quiz: {lesson_id: 1, ...}, score: 100, student: {...}]
// }
const scorePerLesson = quizForStudent.reduce((object, current) => {
// make sure array exist for given lessonId
object[current.quiz.lesson_id] = object[current.quiz.lesson_id] || []
// add array to object
object[current.quiz.lesson_id].push(current)
return object
},{})
// find highest score for each lesson
// {
// 2: {complete: true, quiz: {lesson_id: 2, ...}, score: 100, student: {...}
// 1: {complete: true, quiz: {lesson_id: 1, ...}, score: 100, student: {...}
// }
const highestScorePerLesson = Object.entries(scorePerLesson).reduce((object, [lessonId, quiz]) => ({
...object,
[lessonId]: quiz.reduce((prev, current) => prev.score > current.score ? prev : current)
}), {})
要获得给定课程(例如第 2 课)的最高分,您可以使用
highestScore[2]
在沙盒中签出:link
Array.reduce 可用于此:
const quizArray = [
{user: 6, lesson_id: 2, score: 50, complete: true},
{user: 6, lesson_id: 1, score: 50, complete: true},
{user: 6, lesson_id: 2, score: 50, complete: true},
{user: 6, lesson_id: 2, score: 100, complete: true},
{user: 6, lesson_id: 2, score: 100, complete: true},
{user: 6, lesson_id: 2, score: 100, complete: true}
];
let highScores = Object.values(quizArray
.filter(entry => entry.complete) // Only consider completed entries
.reduce((highScores, entry) => {
// Look up current high score for this lesson
let lesson = highScores[entry.lesson_id];
// If there is no score, or the new score is higher, then switch in new entry
if (!lesson || (entry.score > lesson.score)) { highScores[entry.lesson_id] = entry; }
return highScores;
}, {}));
console.log(highScores);