Created
May 20, 2023 03:06
-
-
Save kironroy/a1dfe73d62ede544770d0d2c09b4003b to your computer and use it in GitHub Desktop.
Destructing Objects
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
'use strict'; | |
// Data needed for a later exercise | |
const flights = | |
'_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30'; | |
// Data needed for first part of the section | |
const restaurant = { | |
name: "Vini's Curry House", | |
location: '123 Gora Way, Mumbai, India', | |
categories: ['Punjabi', 'Bengali', 'Madras', 'Neplai'], | |
starterMenu: ['Samosa', 'Pakora', 'Nan', 'Kebab'], | |
mainMenu: ['Dosa', 'Chicken Madras', 'Tandoori Halibut'], | |
openingHours: { | |
thu: { | |
open: 12, | |
close: 22, | |
}, | |
fri: { | |
open: 11, | |
close: 23, | |
}, | |
sat: { | |
open: 0, // Open 24 hours | |
close: 24, | |
}, | |
}, | |
order: function (starterIndex, mainIndex) { | |
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]]; | |
}, | |
orderDelivery: function ({ starterIndex = 1, mainIndex = 0, time = '24.07', address }) { | |
console.log( | |
`Order received! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered ${address} at ${time}. ` | |
); | |
}, | |
}; | |
restaurant.orderDelivery({ | |
time: '22:30', | |
address: '123 High Five Way', | |
mainIndex: 2, | |
starterIndex: 2, | |
}); | |
restaurant.orderDelivery({ | |
address: '123 High Five Way', | |
starterIndex: 1, | |
}); | |
// Destructuring objects | |
// objects order doesn't matter | |
// good for results from API calls | |
// write a lot less code! | |
const { name, openingHours, categories } = restaurant; | |
console.log(name, openingHours, categories); | |
// variable names different from property names! | |
console.log('---------'); | |
const { | |
name: restaurantName, | |
openingHours: hours, | |
categories: tags, | |
} = restaurant; | |
console.log(restaurantName, hours, tags); | |
// default values | |
const { menu = [], starterMenu: starters = [] } = restaurant; | |
console.log(menu, starters); | |
// mutating variables | |
let a = 111; | |
let b = 999; | |
const obj = { a: 23, b: 7, c: 14 }; | |
// must use () | |
({ a, b } = obj); | |
console.log(a, b); | |
// nested objects | |
const { | |
fri: { open, close }, | |
} = openingHours; | |
// 11 and 23 will print out | |
// representing the hrs of operation | |
console.log(open, close); | |
// cool application of all this! | |
// pass an object as para into a f(x); | |
restaurant.orderDelivery({ | |
time: '22:30', | |
address: '123 High Five Way', | |
mainIndex: 2, | |
starterIndex: 2, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment