Created
March 26, 2019 02:32
-
-
Save micahriggan/2fe963033267bef56422ed3dae01ce63 to your computer and use it in GitHub Desktop.
Some simple destructuring examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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