0

I try to understand the following code (you can copy it to ES 6 Console )

myarr = [{id:'1',title:'title1'},{id:'2',title:'title2'}] ;
mystate = { allVotes: myarr };
console.log(mystate);

const { allVotes } = mystate; //This line I do not understand
console.log(allVotes);

The line

const { allVotes } = mystate; 

seems like a short way to set a variable with the name of the key filled with the value from the object. Can someone this explain? I would do it much uglier with:

const allVotes2 = mystate['allVotes'];

Cristian Ramon-Cortes
  • 1,675
  • 1
  • 17
  • 26

1 Answers1

0

It is called a destructuring assignment and you can read about it here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Thijs
  • 2,281
  • 2
  • 12
  • 21