Nizar Habib
Nizar Habib

Reputation: 5

How to get values from object and assign them to consts

So I'm pulling some data from my DB and it comes out in the form of an object. I'd like to take this object and turn it into separate constants. This is the object
(sorry for link, I dont have enough rep to post images) https://i.sstatic.net/yjWQs.png

How can I take values out from the object and assign them to constants. I want to get the value of the field 'address' which is the apartment's address and assign it to a const called rent.

Upvotes: 0

Views: 595

Answers (3)

Hardik Chaudhary
Hardik Chaudhary

Reputation: 1228

you can access the object properties like,

1.

const {ac, address} = yourObject;

2.

const ac = yourObject.ac;
const address = yourObject.address;

Hope this can help!

Upvotes: 0

Katia Wheeler
Katia Wheeler

Reputation: 549

You can use destructuring. So, let's say you only want the id from the object. You could do something like the following:

const { id } = await fetchMyData();

Upvotes: 1

nbokmans
nbokmans

Reputation: 5747

You can use destructuring for this very easily:

const { rent: address, ownerEmail, otherProperty1, otherProperty2 } = yourObject;
console.log(rent); //The address 

Upvotes: 3

Related Questions