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;
1({ a, b } = { a: 10, b: 20 });
2console.log(a); // 10
3console.log(b); // 20
4
5
6// Stage 4(finished) proposal
7({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
8console.log(a); // 10
9console.log(b); // 20
10console.log(rest); // {c: 30, d: 40}
11
1const hero = {
2 name: 'Batman',
3 realName: 'Bruce Wayne',
4 address: {
5 city: 'Gotham'
6 }
7};
8
9// Object destructuring:
10const { realName, address: { city } } = hero;
11city; // => 'Gotham'
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