-
Notifications
You must be signed in to change notification settings - Fork 1
/
spread_operator.html
153 lines (141 loc) · 3.51 KB
/
spread_operator.html
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html>
<head>
<title>Spread operator</title>
</head>
<body>
<h2>Spread operator</h2>
<script>
/*
Example no 1 -----------------------------
If you have two arrays with shopSellArr1 and shopSellArr2. Now you want to check sell on aug month from two shops.
*/
let shopSellArr1 = [
{
month: 'aug',
sell: '15000'
},
{
month: 'sept',
sell: '25000'
},
{
month: 'oct',
sell: '20000'
}
];
let shopSellArr2 = [
{
month: 'aug',
sell: '17000'
},
{
month: 'sept',
sell: '22000'
},
{
month: 'oct',
sell: '19000'
}
];
let resultArr = [];
shopArr1Aug = shopSellArr1.filter(ele => ele.month === 'aug');
shopArr2Aug = shopSellArr2.filter(ele => ele.month === 'aug');
Object.assign(shopArr1Aug[0], {name: 'Shop 1'});
Object.assign(shopArr2Aug[0], {name: 'Shop 2'});
resultArr.push(...shopArr1Aug, ...shopArr2Aug);
console.table(resultArr);
/*
Example no 2 -----------------------------
Calculate minimum and maximum from an array
*/
let studentArr = [
{
stud_id : 1,
stud_name: 'Student 1',
stud_marks: 60
},
{
stud_id : 2,
stud_name: 'Student 1',
stud_marks: 55
},
{
stud_id : 3,
stud_name: 'Student 1',
stud_marks: 76
},
{
stud_id : 4,
stud_name: 'Student 1',
stud_marks: 87
},
{
stud_id : 5,
stud_name: 'Student 1',
stud_marks: 90
}
];
let marksArr = studentArr.map(ele => ele.stud_marks);
console.log('Marks array => ', marksArr);
let maxMarks = Math.max(...marksArr);
console.log('Maximum marks => ', maxMarks);
let minMarks = Math.min(...marksArr);
console.log('Manimum marks => ', minMarks);
/*
Example no 3 -----------------------------
Print students info with school info. So i have to add "schoolInfo" with studentArr
*/
let studentArr1 = [
{
stud_id : 1,
stud_name: 'Student 1',
stud_marks: 60
},
{
stud_id : 2,
stud_name: 'Student 2',
stud_marks: 55
},
{
stud_id : 3,
stud_name: 'Student 3',
stud_marks: 76
},
{
stud_id : 4,
stud_name: 'Student 4',
stud_marks: 87
},
{
stud_id : 5,
stud_name: 'Student 5',
stud_marks: 90
}
];
let schoolInfo = {
name: 'Don bosco school',
address: 'kolkata, west bengal'
};
let studentInfoArr = [];
studentArr1.forEach(ele => {
let obj = {...ele, ...schoolInfo};
studentInfoArr.push(obj);
});
console.table(studentInfoArr);
/*
Example no 4 -----------------------------
Calling Function without Apply
*/
let txt = ['Hi!', 'How are you ?'];
function showWithApply(txt1, txt2) {
console.log(txt1 + ' ' + txt2);
}
showWithApply.apply(null, txt);
function showWithoutApply(txt1, txt2) {
console.log(txt1 + ' ' + txt2);
}
showWithoutApply(...txt);
</script>
</body>
</html>