An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
April 28, 2021 10:10 pm GMT
Original Link: https://dev.to/rthefounding/use-destructuring-assignment-to-assign-variables-from-nested-objects-28km
Use Destructuring Assignment to Assign Variables from Nested Objects
- Let's take this for example:
const LOCAL_FORECAST = { yesterday: { low: 61, high: 75 }, today: { low: 64, high: 77 }, tomorrow: { low: 68, high: 80 }};
- Here's how to extract the values of object properties and assign them to variables with the same name:
const { today: { low, high }} = LOCAL_FORECAST;
- And here's how you can assign an object properties' values to variables with different names:
const { today: { low: lowToday, high: highToday }} = LOCAL_FORECAST;
console.log(lowToday); will display 64
We just replace the two assignments with an equivalent destructuring assignment. It should still assign the variables lowToday and highToday the values of today.low and today.high from the LOCAL_FORECAST object.
Original Link: https://dev.to/rthefounding/use-destructuring-assignment-to-assign-variables-from-nested-objects-28km
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To