1const person = {
2 name: 'labib',
3 age: 22,
4 job: 'web-developer',
5 frieds: ['ahsik', 'abir', 'alvi', 'hanafi'],
6 childList: {
7 firstChild: 'Salman',
8 secondChild: 'Rafi',
9 thirdChild: 'Anfi'
10 }
11}
12
13//simple destructuring
14const { name, age, job } = person;
15console.log(name, age, job);
16//Expected output: labib 22 web-developer
1const person = {
2 name: 'labib',
3 age: 22,
4 job: 'web-developer',
5 frieds: ['ahsik', 'abir', 'alvi', 'hanafi'],
6 childList: {
7 firstChild: 'Salman',
8 secondChild: 'Rafi',
9 thirdChild: 'Anfi'
10 }
11}
12const { frieds: [a, b, c] } = person; //array destructuring from a nested object
13console.log(a, b, c);
14//expected output: ahsik abir alvi;
15const { childList: { firstChild, secondChild } } = person; //object destructuring from a nested object
16console.log(firstChild, secondChild)
17//expected output:Salman Rafi
1const person = {
2 firstName: 'Adil',
3 lastName: 'Arif',
4 age: 25,
5 job: 'web-developer',
6 love: 'coding',
7 friedsList: {
8 friend1: 'Abir',
9 friend2: 'Adnan'
10 }
11}
12const { friend1, friend2 } = person.friedsList;
13console.log(friend1, friend2);
14//Expected output: Abir Adnan
1const user = { id: 339, name: 'Fred', age: 42};
2const {name} = user;
3console.log(name); //Expected output: fred