Acessing nested objects in javascript

Hi all,
how can I access a nested object without declaring a variable to each property?

const localForecast = {
  yesterday: { low: 61, high: 75 },
  today: { low: 64, high: 77 },
  tomorrow: { low: 68, high: 80 }
};

so the ES6 way of accessing low from yesterday would be:

const {yesterday: { low: yesterdayLow }} = localForecast;
console.log(yesterdayLow);

how can access it directly without having to assign it to a const?

Use the dot notation syntax

1 Like

I tried and didnt work…just one more try…and it worked…

Also object distructuring is more helpful to deeply nested properties and those that aren’t

So prefer the object distructuring over the dot notation if you want simpler code

Care to show an example of destructing objects or arrays that are a few levels deep? Pick your structure