Nested Object Destructuring
You'll learn how to extract values from deeply nested objects using chained destructuring syntax.
When working with complex data structures, you often encounter objects within objects. Instead of drilling down with multiple dot-notation accesses, you can destructure nested properties directly by mirroring the object's structure with colons and additional curly braces.
The Core Pattern
Think of nested destructuring like giving directions to a treasure chest inside a house: "Go to the house, then the bedroom, then the closet, and grab the treasure." You chain the path using colons (:) and curly braces ({}):
const user = {
name: 'Alice',
address: {
city: 'Portland',
zipCode: '97201'
}
};
// Extracting city from nested address object
const { address: { city } } = user;
console.log(city); // 'Portland'
Notice how address: { city } mirrors the structure: "inside address, get city."
Going Deeper
You can nest as many levels as needed:
const data = {
user: {
profile: {
settings: {
theme: 'dark'
}
}
}
};
const { user: { profile: { settings: { theme } } } } = data;
console.log(theme); // 'dark'
Combining Techniques
You can mix renaming, defaults, and nesting:
const product = {
details: {
price: 29.99
}
};
const { details: { price: cost, discount = 0 } } = product;
console.log(cost); // 29.99
console.log(discount); // 0 (default value)
Key Takeaway: Nested object destructuring uses colons and additional curly braces to extract deeply nested properties, following the same structure as the original object—making complex data access clean and readable.