I have a restaurant object where there are nested objects
const restaurant = {
name1: 'Classico Italiano',
location1: 'Via Angelo Tavanti 23, Firenze, Italy',
categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
mainMenu: ['Pizza', 'Pasta', 'Risotto'],
typeofManpower,
parcelAvailable,
openingHours: {
thu: {
open: 12,
close: 22,
},
fri: {
open: 11,
close: 23,
},
sat: {
open: 0, // Open 24 hours
close: 24,
},
},
}
Following is the code where I have shown some object destructuring code.
const {openingHours:{sat:{open:satOpen,close:satClose}}}=restaurant;
console.log(satOpen,satClose);
const {fri:{open:friOpen,close:friClose}}=restaurant.openingHours;
console.log(friOpen,friClose);
const {open:thuOpen,close:thuClose}=restaurant.openingHours.thu;
console.log(thuOpen,thuClose);
I have tried chaining on the RHS. when I remove const from the LHS, it gives error that the variable satOpen/friopen/thuOpen is not declared.
However, I have another object where there are 2 football teams and there is nested object 'odds' with properties
const game = {
team1: 'Bayern Munich',
team2: 'Borrussia Dortmund',
players: [
['Neuer','Pavard','Martinez','Alaba','Davies','Kimmich','Goretzka','Coman', 'Muller','Gnarby','Lewandowski',],
['Burki','Schulz','Hummels','Akanji','Hakimi','Weigl','Witsel','Hazard','Brandt','Sancho','Gotze',],
],
score: '4:0',
scored: ['Lewandowski', 'Gnarby', 'Lewandowski',
'Hummels'],
date: 'Nov 9th, 2037',
odds: {
team1: 1.33,
x: 3.25,
team2: 6.5,
},
printGoals: function(...playerNames){
console.log(playerNames.length);
for(let i in playerNames)
console.log(playerNames[i]);
}
};
I have tried following code for destructuring. However this code works even though there is no const/let on the LHS.
({team1:group1,x:draw,team2:group1} = game.odds);
Hence in the first situation, there is an error when const/let is not used in variable declaration. In second case there is no such error. Please guide.