多选题处理技术求助:如何对比选项获取并展示正确答案
Alright, let's work through this multiple-choice answer matching feature together. Here's a practical, step-by-step approach to pull the correct answers from your question array, match them to your option set, and display the results clearly.
Let’s start with concrete examples to make this tangible. Suppose your question array looks like this (where the 4th and 5th elements are the correct answers—note we’ll use 0-based indexing here, so those correspond to positions 3 and 4):
// Example question array: [question text, distractor1, distractor2, correctAnswer1, correctAnswer2, distractor3] const questionArray = [ "Which of these are primitive JavaScript data types?", "Object", "Function", "String", "Number", "Boolean" ];
And your option set might be a list of formatted options like:
const options = [ "A: Object", "B: Function", "C: String", "D: Number", "E: Boolean" ];
First, we’ll pull the correct answer values from your question array using their specified positions. Let’s hardcode the indices first (you can make this dynamic later if the positions vary per question):
// Indices for the 4th and 5th elements (0-based) const correctAnswerIndices = [3, 4]; // Extract the actual answer texts from the question array const correctAnswerTexts = correctAnswerIndices.map(index => questionArray[index]); // This gives us ["String", "Number"]
Next, we’ll filter the option set to find which options match our correct answer texts. We’ll need to parse each option to get just the text part (ignoring the A/B/C labels):
const matchedCorrectOptions = options.filter(option => { // Split the option into label and text (adjust this logic if your options use a different format) const [_, optionText] = option.split(': '); // Check if this option's text is in our list of correct answers return correctAnswerTexts.includes(optionText); }); // This will return ["C: String", "D: Number"]
If you need to handle case sensitivity or extra spaces, normalize the text first:
const matchedCorrectOptions = options.filter(option => { const [_, optionText] = option.split(': '); return correctAnswerTexts.some(ans => ans.toLowerCase().trim() === optionText.toLowerCase().trim()); });
Finally, you can display the matched correct answers however you need—either in the console for testing, or in a UI element:
// Console output for quick testing console.log("Correct Answers:", matchedCorrectOptions.join(', ')); // Render to a webpage (assuming you have an element with ID 'answer-display') const answerDisplayElement = document.getElementById('answer-display'); if (answerDisplayElement) { answerDisplayElement.innerHTML = `<strong>Correct Answers:</strong> ${matchedCorrectOptions.join(', ')}`; }
- If the position of correct answers in your question array isn’t fixed, consider adding metadata to the question array (like a separate key for
correctAnswerIndices) instead of hardcoding positions. - If your options use a different format (e.g., "1. String" instead of "A: String"), adjust the split logic in the filter step to match.
- For large datasets, optimize matching by converting
correctAnswerTextsinto a Set for faster lookups:const correctAnswerSet = new Set(correctAnswerTexts.map(ans => ans.toLowerCase().trim())); const matchedCorrectOptions = options.filter(option => { const [_, optionText] = option.split(': '); return correctAnswerSet.has(optionText.toLowerCase().trim()); });
内容的提问来源于stack exchange,提问作者Khalid

