Home > Back-end >  Destructuring nested object es6
Destructuring nested object es6

Time:09-23

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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

  • Related