how are you doing? I'd like to remove a property("isCorrect") from a nested object.
Original List
id: 1,
questionText: 'This is a test question for tests',
answerOptions: [
{
answerText: 'A',
isCorrect: true
},
{
answerText: 'B',
isCorrect: false
}
],
difficulty: 1
},
{
id: 2,
questionText: 'This is another test question for tests',
answerOptions: [
{
answerText: 'A',
isCorrect: false
},
{
answerText: 'B',
isCorrect: true
}
],
difficulty: 2
}
Expected result
id: 1,
questionText: 'This is a test question for tests',
answerOptions: [
{
answerText: 'A'
},
{
answerText: 'B'
}
],
difficulty: 1
},
{
id: 2,
questionText: 'This is another test question for tests',
answerOptions: [
{
answerText: 'A'
},
{
answerText: 'B'
}
],
difficulty: 2
}
I managed using delete code below but that is not that best approach
const cleanResponses = (questions: Question[]): Question[] => {
questions.forEach(question => {
question.answerOptions.forEach((answer) => {
delete answer.isCorrect
});
})
return questions;
}
Tried the line below but no success :(
const { answerOptions: [{ isCorrect }], ...rest } = question
Thanks
Using Array#map
:
const arr = [
{ id: 1,
questionText: 'This is a test question for tests',
answerOptions: [ { answerText: 'A', isCorrect: true }, { answerText: 'B', isCorrect: false } ],
difficulty: 1
},
{
id: 2,
questionText: 'This is another test question for tests',
answerOptions: [ { answerText: 'A', isCorrect: false }, { answerText: 'B', isCorrect: true } ],
difficulty: 2
}
];
const res = arr.map(({ answerOptions = [], ...elem }) => ({
...elem,
answerOptions: answerOptions.map(({ isCorrect, ...answer }) => answer)
}));
console.log(res);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments