Skip to content

Instantly share code, notes, and snippets.

@ObsidianCat
Created September 15, 2019 10:54
Show Gist options
  • Save ObsidianCat/f2b749c42a99b97db57dea059764ad57 to your computer and use it in GitHub Desktop.
Save ObsidianCat/f2b749c42a99b97db57dea059764ad57 to your computer and use it in GitHub Desktop.
// Object destructuring
//Object
const response = {
status: 200,
data: {
user: {
name: 'Rachel',
title: 'Editor in Chief'
},
account: {},
company: 'Smashing Magazine'
}
}
//Destructuring second level properties
const { data: {user, company}} = response
//console.log(user, company);
//with renaming
const { data: {user: currentUser}} = response
//console.log(currentUser);
//with renaming and default values (key does not exist in source)
const { data: {location: city = 'New York'}} = response
//console.log(city);
//Destructuring third level
const {data: {user: {name: userName}}} = response
//console.log(userName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment