Javascrip nested object destructuring

Destructuring nested object

We can destroy not only flat objects but also nested objects πŸ‘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Example data
const person = {
name: "hoehoetester",
age: 349,
address: {
street: "123 dokoka",
city: "auckland",
country: "NZ",
},
};

const destruct = () => {
const {
name,
address: { street, country }, // <------- πŸ‘ˆ This is the one
} = person;

console.log("name: " + name);
console.log("street: " + street);
console.log("country: " + country);
};

destruct();

// name: hoehoetester
// street: 123 dokoka
// country: NZ

Ah it’s easy πŸ†πŸ˜„πŸ₯‡