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 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
1var personsInfo = {
2 company: 'sp-coder',
3 persons: [{
4 id: 1,
5 name: 'Adil',
6 friedsList: {
7 friend1: 'Nabil',
8 friend2: 'Habib'
9 }
10 }, {
11 id: 2,
12 name: 'Arif',
13 friedsList: {
14 friend1: 'alvi',
15 friend2: 'avi'
16 }
17 }]
18};
19
20const { friend1, friend2 } = personsInfo.persons[0].friedsList;
21console.log(friend1, friend2);
22// Expected Output: Nabil Habib
1let arr = [1, 2, 3, 4, [100, 200, 300], 5, 6, 7];
2
3// nested array destructuring
4const [a, , , , [, b, ,], , , ,] = arr;
5
6console.log(a);
7// expected output "1"
8
9console.log(b);
10// expected output "200"