Object literas access

Hello there. I’m moving along nicely in my online course but I need help w/ this object literal exercise. I appreciate it.

Exercise explanation:
Object literal help

I’ve tried to access and save the new variable in different ways and I’m stumped w/ what I’ve tried.

Thanks.

Hi, I think something like that could work:

const fullAddress = `${restaurant.address}, ${restaurant.city}, ${restaurant.state} ${restaurant.zipcode}`

If I undenstand it correctly, they just want you to create a new variable which will concatenate the keys of the object so you have the full address in one place. You can use template literals to create such string in javascript.

Since you did not provide any of your non working solution it is hard to guess what you struggle with… feel free to post your solition so we can help you understand what you are doing wrong.

And here is some resource regarding how to work with objects in JS maybe it will help you: Working with objects - JavaScript | MDN

2 Likes

Yes, sorry. Here are some of the code I tried:

let fullAddress = {"address"}
let fullAddress.address
let fullAddress ["address"] (The system didn’t like this one because I didn’t include an =. )

None of those worked.

Hi,
so the problem is that you are trying to access the properties ofthe fullAddress, but variable with that name does not exists.
You need to create a NEW variable with name fullAddress which will concatenate values of the variable (object) with name restaurant .

you can access the values like this:

restaurant.address
restaurant["address"]

and since they want you to create the fullAddress by accessing address, city, state and zipCode, so you can do it like this:

const fullAddress = restaurant.address + ", " + restaurant.city + ", " + restaurant.state + " " + restaurant.zipCode

or you can use template literals:

const fullAddress = `${restaurant.address}, ${restaurant.city}, ${restaurant.state} ${restaurant.zipcode}`

in the first line you shared you probably try to use object destructing, but you are having wrong syntax for that:

const { address, city, state, zipCode } = restaurant

but this will only create a variables with names address, city, state, zipCode and assign values from restaurant object. then you would have to use those to create the fullAddress variables

const fullAddress = address + ", " + city + ", " + state + " " + zipCode

You can learn more about descruting here: Destructuring assignment

I hope it is a more clear now.

1 Like