fix(curriculum): add example for nested object destructuring (#64348)

This commit is contained in:
Huyen Nguyen 2025-12-09 12:23:12 -08:00 committed by GitHub
parent d28fbdf081
commit abcf067746
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -64,6 +64,34 @@ console.log(country); // Unknown
Here, since `country` doesn't exist in our `person` object, it gets the default value `Unknown`.
Another common case is nested object destructuring. You can destructure properties nested inside other objects by using another set of braces:
:::interactive_editor
```js
const recipe = {
name: "Chocolate Cake",
ingredients: {
flour: "2 cups",
sugar: "1 cup"
}
};
// Extract `flour` from `ingredients`
const { ingredients: { flour } } = recipe;
console.log(flour); // "2 cups"
```
:::
This is equivalent to accessing the property directly:
```js
const flour = recipe.ingredients.flour;
console.log(flour); // "2 cups"
```
Now, let's talk about the shorthand notation in object destructuring. When you're creating objects, especially when the property names match variable names, you can use a shorthand syntax:
:::interactive_editor