I just started learning javascript and I'm trying to add an element to an object I previously created but I get an error that property does not exist.
I cant seem to find the issue, maybe its a syntax error but i cant seem to see it so maybe someone can help me spot it if thats the case?
This is the object >
const restaurant = {
name: 'Classico Italiano',
location: 'Via Angelo Tavanti 23, Firenze, Italy',
categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
mainMenu: ['Pizza', 'Pasta', 'Risotto'],
openingHours: {
thu: {
open: 12,
close: 22,
},
fri: {
open: 11,
close: 23,
},
sat: {
open: 0, // Open 24 hours
close: 24,
},
},
orderD: function ({
orderName = 'ANON',
address = 'ANON',
time = '00:00',
order = 'ANON',
}) {
console.log(`${orderName} has ordered (${order}) to ${address} at ${time}`);
},
orderPizza: function (mainIngredient, ...otheringredients) {
console.log(mainIngredient);
console.log(otheringredients);
},
};
Then here I tried to add an element to it.
restaurant.numOfGuests = 0;
but then it highlights the element name saying
Property 'numOfGuests' does not exist on type '{ name: string; location: string; categories: string[]; starterMenu: string[]; mainMenu: string[]; openingHours: { thu: { open: number; close: number; }; fri: { open: number; close: number; }; sat: { open: number; close: number; }; }; orderD: ({ orderName, address, time, order, }: { ...; }) => void; orderPizza: (mai...'.ts(2339)
I tried to ignore it and move on but then when i tried logging the new element to the console
console.log(numOfGuests);
It gave me this error
Uncaught ReferenceError: numOfGuests is not defined
It doesn't appear that you have set the numOfGuests property, which is important if your code is TypeScript (as opposed to JavaScript). All you have to do is set it as in the following code. Then, you can reference it in any other code as needed. See Object Types for more information.
const restaurant = {
name: 'Classico Italiano',
location: 'Via Angelo Tavanti 23, Firenze, Italy',
categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
mainMenu: ['Pizza', 'Pasta', 'Risotto'],
numOfGuests: 20,
openingHours: {
thu: {
open: 12,
close: 22,
},
fri: {
open: 11,
close: 23,
},
sat: {
open: 0, // Open 24 hours
close: 24,
},
},
orderD: function({
orderName = 'ANON',
address = 'ANON',
time = '00:00',
order = 'ANON',
}) {
console.log(`${orderName} has ordered (${order}) to ${address} at ${time}`);
},
orderPizza: function(mainIngredient, ...otheringredients) {
console.log(mainIngredient);
console.log(otheringredients);
},
};
console.log(restaurant);
If you're using JavaScript, you shouldn't get the error. See the following code snippet as an example:
const restaurant = {
name: "Classico Italiano",
location: "Via Angelo Tavanti 23, Firenze, Italy",
categories: ["Italian", "Pizzeria", "Vegetarian", "Organic"],
starterMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"],
mainMenu: ["Pizza", "Pasta", "Risotto"],
openingHours: {
thu: {
open: 12,
close: 22
},
fri: {
open: 11,
close: 23
},
sat: {
open: 0, // Open 24 hours
close: 24
}
},
orderD: function({
orderName = "ANON",
address = "ANON",
time = "00:00",
order = "ANON"
}) {
console.log(`${orderName} has ordered (${order}) to ${address} at ${time}`);
},
orderPizza: function(mainIngredient, ...otheringredients) {
console.log(mainIngredient);
console.log(otheringredients);
}
};
restaurant.numOfGuests = 20;
console.log(restaurant);