I'm currently working on figuring out the proper syntax for sorting an object by ID and then identifying the highest score within that ID array. While I've managed to sort the object up to this point using conditionals and the sort() method, I'm looking to streamline it further by pinpointing the highest score based on lesson_id. What am I overlooking?
Here's what I have achieved so far:
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}
However, my desired output is:
0: {user: 6, lesson_id: 2, score: 100, complete: true}
1: {user: 6, lesson_id: 1, score: 50, complete: true}
This is what I have accomplished so far using 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);
//uncertain 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
});
},
Below is the original object structure for reference:
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, …}