Reputation: 5
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
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
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
Reputation: 5747
You can use destructuring for this very easily:
const { rent: address, ownerEmail, otherProperty1, otherProperty2 } = yourObject;
console.log(rent); //The address
Upvotes: 3