Skip to content

Instantly share code, notes, and snippets.

@oliversd
Created January 6, 2019 17:50
Show Gist options
  • Save oliversd/1186c5f1dff41972f041e2c7804fba6a to your computer and use it in GitHub Desktop.
Save oliversd/1186c5f1dff41972f041e2c7804fba6a to your computer and use it in GitHub Desktop.
// Old method
const myArray = ['apple', 'pear', 'orange', 'banana'];
let fruit1 = myArray[0];
let fruit2 = myArray[1];
let fruit3 = myArray[2];
let fruit4 = myArray[3];
//ES6 destructuring
let [fruit1, fruit2, fruit3, fruit4] = myArray; // much better isn't? 
// We can use it on objects to
let dog = {
 name: 'Toby',
 age: 3,
 breed: 'Beagle',
 features: {
 color: 'White and brown',
 favoriteToy: 'Plastic duck'
 }
}
// We can obtain the values like this with destructuring
let {name, age, breed} = dog;
// What if we want only name and age and all the other in another variable
let {name, age, …info} = dog;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment