Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
Destructuring nested object es6

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report

0

It depends if you want to mutate the existing array, or create a new array. If it's the former your code is fine (I've provided an updated version below). If it's the latter Majed Badawi has provided a good answer using map. Either way you're going to be iterating over both the objects in the data array and the objects in the answerOptions array to get the result you need.

const arr=[{id:1,questionText:"This is a test question for tests",answerOptions:[{answerText:"A",isCorrect:!0},{answerText:"B",isCorrect:!1}],difficulty:1},{id:2,questionText:"This is another test question for tests",answerOptions:[{answerText:"A",isCorrect:!1},{answerText:"B",isCorrect:!0}],difficulty:2}];

for (let { answerOptions } of arr) {
  for (let obj of answerOptions) {
    delete obj.isCorrect;
  }
}

console.log(arr);

about 4 years ago · Juan Pablo Isaza Report

0

Here is another (longer) way to achieve the desired output. Working from out to in, first we map across all objects in the array test. I use the rest operator to separate the array of answerOptions objects from the other question properties. Next, we need to map across the answerOptions array. Using the rest operator, create a new answerOption object that does not contain the isCorrect property. Finally, we bring it all back together. Return a object that contains the separated question properties and the new answerOptions objects without isCorrect.

  const test = [
    {
      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 cleanResponses = (questions) => {
    return questions.map(({ answerOptions, ...rest }) => {
      const newAnswerOptions = answerOptions.map((answerOption) => {
        const { isCorrect, ...newAnswerOption } = answerOption;
        return newAnswerOption;
      });
      return { answerOptions: { ...newAnswerOptions }, ...rest };
    });
  };
  
  console.log(cleanResponses(test))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!