-
Notifications
You must be signed in to change notification settings - Fork 0
/
array destructuring.js
78 lines (59 loc) · 1.88 KB
/
array destructuring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
// the idea of destructuring is to take an object or an Array and convert it into
// smaller objects/elements/variables
// array destructuring
// console.log('----------------Array destructuring---------------------');
// const alphabet= ['A','B','C','D','E','F'];
// const numbers = ['1','2','3','4','5','6'];
// // old way
// // const a = alphabet[0];
// // const b = alphabet[1];
// // destructuring
// // if we want the rest of the elemnt we use the spread operator ...
// const [a,,c,...rest] = alphabet;
// const newArray = [...alphabet, ...numbers];
// console.log(a,c);
// console.log(rest);
// console.log(newArray);
// function sumAndMultiply(a,b){
// return [a+b, a*b, a/b];
// }
// // const arr = sumAndMultiply(2,3);
// const [sum, multiply, division = 'No division'] = sumAndMultiply(2,3);
// console.log(sum);
// console.log(multiply);
// console.log(division);
console.log('--------------------------Object destructuring--------------------------');
// Object destructuring
const personOne = {
name: 'Nelson',
age: 22,
address: {
city: 'Bhaktapur',
province: 'Province 3'
}
}
// const personTwo = {
// age: 24,
// favoriteFood: 'Watermelon'
// }
function printUser({ name, age, favoriteFood = 'Momo'}){
console.log(`Name is ${name} and age is ${age}, and food i love is ${favoriteFood}.`);
}
printUser(personOne);
// const personTwo = {
// name: 'Luffy',
// age: 24,
// address: {
// city: 'Foosha',
// province: 'Japan'
// }
// }
// combining two objects. anything that was in person 1 will be overwritten by person two
// const personThree = { ...personOne, ...personTwo};
// const { name: firstName , ...other} = personTwo;
// destrucutre nested object
// const { name: firstName , address: { city }} = personTwo;
// console.log(firstName);
// console.log(city);
// console.log(personThree);