You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多选题处理技术求助:如何对比选项获取并展示正确答案

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.

Step 1: Define Your Data Structures First

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"
];
Step 2: Extract Correct Answer Texts

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"]
Step 3: Match Correct Answers to the Option Set

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());
});
Step 4: Display the Results

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(', ')}`;
}
Edge Cases to Keep in Mind
  • 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 correctAnswerTexts into 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:37:27