DEV Community

Evangelos Kolimitras
Evangelos Kolimitras

Posted on

How to destructure a variable nested inside an object's array?

https://thepracticaldev.s3.amazonaws.com/i/yhyl6hjezy5ctrjqzetm.png

Top comments (1)

Collapse
 
monicat profile image
Monica Macomber

If you want to destructure BUILDING to get room2, you could do this:

const [,,{floor: { rooms: { room2 }}}] = Building;

The first two commas skip over the first two objects inside your array. Then you just destructure objects until you get to the value you want to use.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay