1const book = {
2 title: 'Ego is the Enemy',
3 author: 'Ryan Holiday',
4 publisher: {
5 name: 'Penguin',
6 type: 'private'
7 }
8};
9
10const {title: bookName = 'Ego', author, name: {publisher: { name }} = book, type: {publisher: { type }} = book } = book;
1Object Destructuring =>
2//
3 The destructuring assignment syntax is a JavaScript expression that makes it
4possible to unpack values from arrays,
5or properties from objects, into distinct variables.
6//
7example:
8const user = {
9 id: 42,
10 is_verified: true
11};
12
13const {id, is_verified} = user;
14
15console.log(id); // 42
16console.log(is_verified); // true
17
1const wes = {
2 first: 'Wes',
3 last: 'Bos',
4 links: {
5 social: {
6 twitter: 'https://twitter.com/wesbos',
7 facebook: 'https://facebook.com/wesbos.developer',
8 },
9 web: {
10 blog: 'https://wesbos.com'
11 }
12 }
13};
14
15//destructuring
16const { twitter, facebook } = wes.links.social;
17console.log(twitter, facebook); // logs the 2 variables
1let {name, country, job} = {name: "Sarah", country: "Nigeria", job: "Developer"};
2
3console.log(name);//"Sarah"
4console.log(country);//"Nigeria"
5console.log(job);//Developer"
6