1// destructuring array & nested array & combine array into single array
2let person = ["John", "Sandy", "Sam", ["Mike", "Max"], "Diego", "Paul"];
3// empty comma is like skipping array index. I skipped "Sam"
4const [a, b, , c, ...d] = person;
5
6let friend = [d, "Tom", "Jerry"]
7let newFriend = [...d, "Tom", "Jerry"]
8
9console.log(a); // output: "John"
10console.log(b); // output: "Sandy"
11console.log(c); // output: [ "Mike", "Max" ]
12console.log(d); // output: ["Diego", "Paul"]
13console.log(friend); // output: [ [ 'Diego', 'Paul' ], 'Tom', 'Jerry' ]
14console.log(newFriend); // output: [ 'Diego', 'Paul', 'Tom', 'Jerry' ]
15
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