I have a table that tracks all user responses to a quiz. This query will successfully get all the data from that table and put it into my TotalQuizResponses array which I then use in my view:
this.totalQuizResponses = ((await QuizResponsesService.index()).data)
However, I'd also like to display the total number of matches in my view. I know this SQL query successfully gets that total number of matches from the table data:
SELECT count(*)
FROM QuizResponses A, QuizResponses B
WHERE A.answerKey = B.answerKey AND A.QuizId = B.QuizId AND A.UserId < B.UserId
I don't want to have to do another service call just to get the count on filtered data that I already have inside my view. Is there a way to somehow filter the data from within the view? I'm unsure how to replicate this logic using pure JavaScript. Thanks for reading.
CodePudding user response:
If you need the exact same query on unfiltered data:
countResponses(this.totalQuizResponses);
function countResponses(quizData) {
let counter = 0;
for (const quizResponseA of quizData) {
for (const quizResponseB of quizData) {
if (quizResponseA === quizResponseB) { continue; }
const isSameAnswerKey = quizResponseA.answerKey === quicResponseB.answerKey;
const isSameQuizId = quizResponseA.QuizId === quizResponseB.QuizId;
const isSameUser = quizResponseA.UserId === quizResponseB.UserId;
if (isSameAnswerKey && isSameQuizzId && isSameUser) {
counter ;
}
}
}
return counter;
}
If you already filtered your data with the same query, this.totalQuizResponses.length
is the same as a counter for all the data you have locally