I am trying to find the correct syntax for sorting an object by id and then finding the highest score for that id array. I have sorted the object so far to this point with conditionals and the sort() method but I want to condense the object to the highest score by lesson_id. What am I missing?
Heres what Im getting at this point 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}
But I want it like this:
0: {user: 6, lesson_id: 2, score: 100, complete: true}
1: {user: 6, lesson_id: 1, score: 50, complete: true}
Heres what Ive got so far (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
});
},
Here's the original object:
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, …}