Skip to content

Instantly share code, notes, and snippets.

@micahriggan
Created March 26, 2019 02:32
Show Gist options
  • Save micahriggan/2fe963033267bef56422ed3dae01ce63 to your computer and use it in GitHub Desktop.
Save micahriggan/2fe963033267bef56422ed3dae01ce63 to your computer and use it in GitHub Desktop.
Some simple destructuring examples
function getPeople() {
return [{name: "Bob"}, {name: "Sam"}, {name: "Tom"}];
}
function test1() {
const [firstPerson] = getPeople();
console.log(firstPerson);
}
function test2() {
const [firstPerson, ...rest] = getPeople();
console.log(rest);
}
function test3() {
const [{name}] = getPeople();
console.log(name);
}
function test4() {
const [{name: firstName}] = getPeople();
console.log(firstName);
}
function test5() {
const [_, secondPerson] = getPeople();
console.log(secondPerson);
}
test1();
test2();
test3();
test4();
test5();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment