mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-07-01 21:01:20 +08:00
fix(curriculum): add example for nested object destructuring (#64348)
This commit is contained in:
parent
d28fbdf081
commit
abcf067746
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user