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 variable…


This content originally appeared on DEV Community and was authored by Randy Rivera

  • 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.


This content originally appeared on DEV Community and was authored by Randy Rivera


Print Share Comment Cite Upload Translate Updates
APA

Randy Rivera | Sciencx (2021-04-28T22:10:25+00:00) Use Destructuring Assignment to Assign Variables from Nested Objects. Retrieved from https://www.scien.cx/2021/04/28/use-destructuring-assignment-to-assign-variables-from-nested-objects/

MLA
" » Use Destructuring Assignment to Assign Variables from Nested Objects." Randy Rivera | Sciencx - Wednesday April 28, 2021, https://www.scien.cx/2021/04/28/use-destructuring-assignment-to-assign-variables-from-nested-objects/
HARVARD
Randy Rivera | Sciencx Wednesday April 28, 2021 » Use Destructuring Assignment to Assign Variables from Nested Objects., viewed ,<https://www.scien.cx/2021/04/28/use-destructuring-assignment-to-assign-variables-from-nested-objects/>
VANCOUVER
Randy Rivera | Sciencx - » Use Destructuring Assignment to Assign Variables from Nested Objects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/28/use-destructuring-assignment-to-assign-variables-from-nested-objects/
CHICAGO
" » Use Destructuring Assignment to Assign Variables from Nested Objects." Randy Rivera | Sciencx - Accessed . https://www.scien.cx/2021/04/28/use-destructuring-assignment-to-assign-variables-from-nested-objects/
IEEE
" » Use Destructuring Assignment to Assign Variables from Nested Objects." Randy Rivera | Sciencx [Online]. Available: https://www.scien.cx/2021/04/28/use-destructuring-assignment-to-assign-variables-from-nested-objects/. [Accessed: ]
rf:citation
» Use Destructuring Assignment to Assign Variables from Nested Objects | Randy Rivera | Sciencx | https://www.scien.cx/2021/04/28/use-destructuring-assignment-to-assign-variables-from-nested-objects/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.