Skip to content

Instantly share code, notes, and snippets.

@goldhand
Created February 15, 2017 17:32
Show Gist options
  • Select an option

  • Save goldhand/5c3d77cbccd23c7b76c82a54050d712c to your computer and use it in GitHub Desktop.

Select an option

Save goldhand/5c3d77cbccd23c7b76c82a54050d712c to your computer and use it in GitHub Desktop.
Examples of es6 array destructure assignments
// Array Destructure Assignment Examples
const myArray = ['a', ['b', ['c']], ['d']];
const oldWayA = myArray[0]; // 'a'
const [newWayA] = myArray; // 'a'
const oldWayB = myArray[1][0][0]; // 'b'
const [,[newWayB]] = myArray; // 'b'
const oldWayC = myArray[1][1][0]; // 'c'
const [,[,[newWayC]]] = myArray; // 'c'
console.log(oldWayA, newWayA); // 'a' 'a'
console.log(oldWayB, newWayB); // 'b' 'b'
console.log(oldWayC, newWayC); // 'c' 'c'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment