Created
February 3, 2016 16:36
-
-
Save juan-m-medina/f34e5b4ceabea3091ad0 to your computer and use it in GitHub Desktop.
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
// Simple one to one assignment | |
var [a, b, c] = [1, 2, 3]; | |
console.log(`a is ${a}`); | |
console.log(`b is ${b}`); | |
console.log(`c is ${c}`); | |
// Assigning the remaining elements | |
var [alpha, beta, gamma, ...theRest] = [1,2,3,4,5,6,7,8,9,10]; | |
console.log(`alpha is ${alpha}`); | |
console.log(`beta is ${beta}`); | |
console.log(`gamma is ${gamma}`); | |
console.log(`theRest is ${theRest}`); | |
// Skipping elements | |
var [one, , three, , five] = [1,2,3,4,5]; | |
console.log(one); | |
console.log(three); | |
console.log(five); | |
// Direct pull of object parameter properties | |
function fullName({ first: firstName, last: lastName}) { | |
return `${firstName} ${lastName}`; | |
} | |
var awesomestPerson = { first: 'Juan', last: 'Medina'}; | |
console.log(`The most awesome person in the whole world is ${fullName(awesomestPerson)}`); | |
// Defaulting object parameters and the full object | |
function carelessFullName({ first: firstName = 'Marty', last: lastName = 'McFly'} = { first: 'John', last: 'Doe'}) { | |
//function carelessFullName({ first: firstName = 'Marty', last: lastName = 'McFly'} = { first: firstName = 'John', last: lastName = 'Doe'}) { | |
return `${firstName} ${lastName}`; | |
} | |
var juanToTheFuture = {first: 'Juan'}; | |
var martyFromColombia = {last: 'Medina'} | |
var nobodyCares = {}; | |
console.log(`juanToTheFuture is ${carelessFullName(juanToTheFuture)}`); | |
console.log(`martyFromColombia is ${carelessFullName(martyFromColombia)}`); | |
console.log(`nobodyCared is ${carelessFullName(nobodyCares)}`); | |
console.log(`empty Parameter yields ${carelessFullName()}`); | |
// Object destructuring | |
var elGrandeBoardGame = { | |
title : 'El Grande', | |
authors : [ { name :'Wolfgang Kramer'}], | |
categories : ['Area Control', 'Spain', 'Euro'], | |
publisher : [ { name: 'Rio Grande Games', country : 'United States' }, { name: 'Libelund' , country: 'France'} ] | |
} | |
var { title : title, authors: [{ name : oneAuthor}] , categories: [firstCategory], publisher : [ { name :firstPublisher}] } = elGrandeBoardGame; | |
console.log(`${title} by ${oneAuthor} is a(n) ${firstCategory} game published by ${firstPublisher}`); | |
var test = []; | |
console.log(test instanceof Array); | |
var dictionary = [ | |
{ key: 'first', value :'1' }, | |
{ key: 'second', value :'2' }, | |
{ key: 'third', value :'3' }, | |
{ key: 'fourth', value :'4' } | |
]; | |
console.log(dictionary instanceof Array); | |
console.log(dictionary); | |
for (let { key: key } of dictionary) { | |
console.log(key); | |
} | |
var supers = [ | |
{ firstName: 'Kal', lastName :'El', heroName: 'Superman', powers : ['Superstrength', 'X-Ray vision'] }, | |
{ firstName: 'Bruce', lastName :'Wayne', heroName: 'Batman', powers : ['Gadgets', 'Detective', ''] }, | |
{ firstName: 'Peter', lastName :'Parker', heroName: 'Spiderman', powers : ['Spider Sense', 'Super Agility'] } | |
]; | |
for (let { heroName : superHero } of supers) { | |
console.log(superHero) | |
} | |
for (let test in supers) { | |
console.log(test); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment