Deep object destructuring in JavaScript

Let's assume that you have the following object:

js
let erik = {
    age: 27,
    location: {
        city: 'Barcelona',
        country: 'Spain'
    }
}

If you want to perform a deep destructure of the country property, then:

js
let { location: { country } } = erik
// Equivalent to => let country = erik.location.country

console.log(country)
// Spain

You could destructure 2 properties at the same time:

js
let { location: { city, country } } = erik
// Equivalent to => let country = erik.location.country

console.log(city, country)
// Barcelona Spain

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.