-2

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.

Linus
  • 39
  • 4
  • Could you format the question more clear and the object structure as well. – Aayush Mall Jun 05 '21 at 17:49
  • please add the content of `restaurant` in literal notation. what is the wanted result of all? – Nina Scholz Jun 05 '21 at 17:50
  • Linus your question needs some clarity. I am not able to distinguish what kind of objet you have nested in the restaurant object. Please use the Stackoverflow editor's tool to format the code and maybe present your query in some readable form. – jateen Jun 05 '21 at 17:51

1 Answers1

0

I looks like, you are resuing the same variables (open, close). This is not possible by declaring them again with const or let in the same block.

In opposite of var, where the declaring is hoisted to start of the code or function.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Nina, even though open and close are declared multiple times, the values are immediately assigned to different variables making open and close free again. The function is working with const or let. Problem appears when they are removed. In second case of code, ({team1:group1,x:draw,team2:group1} = game.odds); is working just fine without const or let. – Linus Jun 05 '21 at 19:35