6
6
< body >
7
7
< h2 > Spread operator</ h2 >
8
8
< script >
9
- let arr = [ 1 , 2 , 3 , 4 , 5 ] ;
10
- let copyarr = [ ] ;
11
- copyarr . push ( arr ) ;
12
- console . log ( 'copyarr => ' , copyarr ) ;
13
- console . log ( 'copyarr with spread operator => ' , ...copyarr ) ;
9
+ /*
10
+ Example no 1 -----------------------------
11
+ If you have two arrays with shopSellArr1 and shopSellArr2. Now you want to check sell on aug month from two shops.
12
+ */
13
+ let shopSellArr1 = [
14
+ {
15
+ month : 'aug' ,
16
+ sell : '15000'
17
+ } ,
18
+ {
19
+ month : 'sept' ,
20
+ sell : '25000'
21
+ } ,
22
+ {
23
+ month : 'oct' ,
24
+ sell : '20000'
25
+ }
26
+ ] ;
27
+ let shopSellArr2 = [
28
+ {
29
+ month : 'aug' ,
30
+ sell : '17000'
31
+ } ,
32
+ {
33
+ month : 'sept' ,
34
+ sell : '22000'
35
+ } ,
36
+ {
37
+ month : 'oct' ,
38
+ sell : '19000'
39
+ }
40
+ ] ;
41
+
42
+ let resultArr = [ ] ;
43
+ shopArr1Aug = shopSellArr1 . filter ( ele => ele . month === 'aug' ) ;
44
+ shopArr2Aug = shopSellArr2 . filter ( ele => ele . month === 'aug' ) ;
45
+ Object . assign ( shopArr1Aug [ 0 ] , { name : 'Shop 1' } ) ;
46
+ Object . assign ( shopArr2Aug [ 0 ] , { name : 'Shop 2' } ) ;
47
+ resultArr . push ( ...shopArr1Aug , ...shopArr2Aug ) ;
48
+ console . table ( resultArr ) ;
49
+
50
+ /*
51
+ Example no 2 -----------------------------
52
+ Calculate minimum and maximum from an array
53
+ */
54
+
55
+ let studentArr = [
56
+ {
57
+ stud_id : 1 ,
58
+ stud_name : 'Student 1' ,
59
+ stud_marks : 60
60
+ } ,
61
+ {
62
+ stud_id : 2 ,
63
+ stud_name : 'Student 1' ,
64
+ stud_marks : 55
65
+ } ,
66
+ {
67
+ stud_id : 3 ,
68
+ stud_name : 'Student 1' ,
69
+ stud_marks : 76
70
+ } ,
71
+ {
72
+ stud_id : 4 ,
73
+ stud_name : 'Student 1' ,
74
+ stud_marks : 87
75
+ } ,
76
+ {
77
+ stud_id : 5 ,
78
+ stud_name : 'Student 1' ,
79
+ stud_marks : 90
80
+ }
81
+ ] ;
82
+
83
+ let marksArr = studentArr . map ( ele => ele . stud_marks ) ;
84
+ console . log ( 'Marks array => ' , marksArr ) ;
85
+ let maxMarks = Math . max ( ...marksArr ) ;
86
+ console . log ( 'Maximum marks => ' , maxMarks ) ;
87
+ let minMarks = Math . min ( ...marksArr ) ;
88
+ console . log ( 'Manimum marks => ' , minMarks ) ;
89
+
90
+
91
+ /*
92
+ Example no 3 -----------------------------
93
+ Print students info with school info. So i have to add "schoolInfo" with studentArr
94
+ */
95
+
96
+ let studentArr1 = [
97
+ {
98
+ stud_id : 1 ,
99
+ stud_name : 'Student 1' ,
100
+ stud_marks : 60
101
+ } ,
102
+ {
103
+ stud_id : 2 ,
104
+ stud_name : 'Student 2' ,
105
+ stud_marks : 55
106
+ } ,
107
+ {
108
+ stud_id : 3 ,
109
+ stud_name : 'Student 3' ,
110
+ stud_marks : 76
111
+ } ,
112
+ {
113
+ stud_id : 4 ,
114
+ stud_name : 'Student 4' ,
115
+ stud_marks : 87
116
+ } ,
117
+ {
118
+ stud_id : 5 ,
119
+ stud_name : 'Student 5' ,
120
+ stud_marks : 90
121
+ }
122
+ ] ;
123
+
124
+ let schoolInfo = {
125
+ name : 'Don bosco school' ,
126
+ address : 'kolkata, west bengal'
127
+ } ;
128
+ let studentInfoArr = [ ] ;
129
+ studentArr1 . forEach ( ele => {
130
+ let obj = { ...ele , ...schoolInfo } ;
131
+ studentInfoArr . push ( obj ) ;
132
+ } ) ;
133
+
134
+ console . table ( studentInfoArr ) ;
135
+
14
136
</ script >
15
137
</ body >
16
138
</ html >
0 commit comments