Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda