Destructuring of objects

const restaurant = {
  name: "Classico Italiano",
  location: "Via Angelo Tavanti 23, Firenze, Italy",
  categories:["Italian", "Pizzeria","Vegetarian", "Organic"],
  startMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"],
  mainMenu: ["Pizza", "Pasta", "Risotto"],
  openingHours: {
    thur: {
      open: 12,
      close:22
    },
    fri: {
      open: 11,
      close: 23
    },
    sat: {
      open: 0, //Open 24 hours
      close: 24,
    },
  },
  order: function (starterIndex, mainIndex){
    return [this.startMenu[starterIndex], this.mainMenu[mainIndex]]
  }
}

i am trying to destructure the name, location and opening hours like this

const {name, location, categories} = restaurant;
console.log(name, location, categories);

but i am getting the below error

Uncaught SyntaxError: Identifier 'location' has already been declared (at script.js:1:1)

i am guessing it is because location is a keyword in javascript.
Please i need help with this

have you tried de structuring and giving it a new variable name?

const {name, location: place, categories} = restaurant;
console.log(name, place, categories);

yea its already on the window object encapsulate it in a scope and it should work or rename it to another variable

it worked with giving it a new variable name. Thank you

i renamed it and it worked. Thank you

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.