Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring destructuring statement

const question = this.props.question
const answer = question.answer

Refactor the above snippet to use destructuring.

const { question } = this.props
const { answer } = question

How can I refactor this destructuring to one line? If I do this one, const { question : { answer } } = this.props, I won't get the reference for question?

like image 390
Henok Tesfaye Avatar asked Jan 26 '23 14:01

Henok Tesfaye


1 Answers

You can reference the same property multiple times when destructuring:

const { question, question: { answer } } = this.props;

const props = {
  question: {
    answer: 'answer'
  }
};
const { question, question: { answer } } = props;
console.log(question);
console.log(answer);
like image 77
CertainPerformance Avatar answered Jan 28 '23 10:01

CertainPerformance