I have an array with Questions. Each question has options to pick from and the correct answer. I am trying to add true/false if the option is correct. How should I best do this? I tried .map()
and .forEach
but I don't know how to access correct_answer
and use that to add the true/false
values under options.
const data = [
{
"question": "What is the approximate value of mathematical constant e?",
"options": [
{
"option": "1.41",
},
{
"option": "1.62",
},
{
"option": "2.72",
},
{
"option": "3.14",
}
],
"correct_answer": "2.72"
},
{
"question": "What is the name of the main character of the anime "One-Punch Man"?",
"options": [
{
"option": "Genos",
},
{
"option": "King",
},
{
"option": "Saitama",
},
{
"option": "Sonic",
}
],
"correct_answer": "Saitama"
}
]
Desired result
const data = [
{
"question": "What is the approximate value of mathematical constant e?",
"options": [
{
"option": "1.41",
"correct_answer": false,
},
{
"option": "1.62",
"correct_answer": false,
},
{
"option": "2.72",
"correct_answer": true,
},
{
"option": "3.14",
"correct_answer": false,
}
],
"correct_answer": "2.72"
},
{
"question": "What is the name of the main character of the anime "One-Punch Man"?",
"options": [
{
"option": "Genos",
"correct_answer": false,
},
{
"option": "King",
"correct_answer": false,
},
{
"option": "Saitama",
"correct_answer": true,
},
{
"option": "Sonic",
"correct_answer": false,
}
],
"correct_answer": "Saitama"
}
]
CodePudding user response:
You can use Array.map
to update the values and the spread
operator to populate the objects.
let newdata = data.map(d => ({ ...d,
options: d.options.map(m => ({ ...m,
correct_answer: m.option == d.correct_answer ? true : false
}))
}))
const data = [{
"question": "What is the approximate value of mathematical constant e?",
"options": [{
"option": "1.41",
},
{
"option": "1.62",
},
{
"option": "2.72",
},
{
"option": "3.14",
}
],
"correct_answer": "2.72"
},
{
"question": "What is the name of the main character of the anime "One-Punch Man"?",
"options": [{
"option": "Genos",
},
{
"option": "King",
},
{
"option": "Saitama",
},
{
"option": "Sonic",
}
],
"correct_answer": "Saitama"
}
]
let newdata = data.map(d => ({ ...d,
options: d.options.map(m => ({ ...m,
correct_answer: m.option == d.correct_answer ? true : false
}))
}))
console.log(newdata)
CodePudding user response:
const data = [{
"question": "What is the approximate value of mathematical constant e?",
"options": [{
"option": "1.41",
},
{
"option": "1.62",
},
{
"option": "2.72",
},
{
"option": "3.14",
}
],
"correct_answer": "2.72"
},
{
"question": "What is the name of the main character of the anime "One-Punch Man"?",
"options": [{
"option": "Genos",
},
{
"option": "King",
},
{
"option": "Saitama",
},
{
"option": "Sonic",
}
],
"correct_answer": "Saitama"
}
]
data.forEach(question => {
question.options.forEach(option => {
option.correct_answer = option.option === question.correct_answer
})
})
console.log(data)
CodePudding user response:
To achieve the desired result, you can use the map()
method to iterate over the data array, and the forEach()
method to iterate over the options array. In the callback function passed to forEach()
, you can use an if statement to check if the current option is equal to the correct answer, and set the correct_answer property to true or false accordingly.
Here is an example of how you can implement this:
const data = [
{
"question": "What is the approximate value of mathematical constant e?",
"options": [
{
"option": "1.41",
},
{
"option": "1.62",
},
{
"option": "2.72",
},
{
"option": "3.14",
}
],
"correct_answer": "2.72"
},
{
"question": "What is the name of the main character of the anime "One-Punch Man"?",
"options": [
{
"option": "Genos",
},
{
"option": "King",
},
{
"option": "Saitama",
},
{
"option": "Sonic",
}
],
"correct_answer": "Saitama"
}
];
const updatedData = data.map(datum => {
const options = datum.options.map(option => {
if (option.option === datum.correct_answer) {
return {...option, correct_answer: true};
} else {
return {...option, correct_answer: false};
}
});
return {...datum, options};
});
console.log(updatedData);
In the code above, we first use map()
to iterate over the data array. For each object in the data array, we use the map()
method again to iterate over the options array. In the callback function passed to map()
, we check if the option property of the current option is equal to the correct_answer property of the current datum. If they are equal, we return a new object with the same properties as the current option, plus a new property correct_answer set to true. If they are not equal, we return a new object with the same properties as the current option, plus a new property correct_answer set to false.
After the inner map()
call is done, we return a new object with the same properties as the current datum, plus a new property options set to the updated array of options.
Finally, we print the updated data array to the console. You can modify this code to fit your specific needs.