Skip to content

Commit eeb1c7d

Browse files
committed
spread operator + function + object clone
1 parent 0977f58 commit eeb1c7d

File tree

3 files changed

+172
-5
lines changed

3 files changed

+172
-5
lines changed

Object_clone_ways.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>3 ways to clone object</title>
5+
</head>
6+
<body>
7+
<h2>3 ways to clone object</h2>
8+
<script>
9+
let person = {
10+
name: 'Piyali',
11+
dept: 'IT'
12+
};
13+
14+
// spread operator
15+
console.log('Clone object using spread operator => ', {...person});
16+
17+
// Object.assign
18+
console.log('Clone object using Object.assign => ', Object.assign({}, person));
19+
20+
// JSON
21+
console.log('Clone object using spread operator => ', JSON.parse(JSON.stringify(person)));
22+
</script>
23+
</body>
24+
</html>

function_constructor.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Function Constructor</title>
5+
</head>
6+
<body>
7+
<h2>Function Constructor</h2>
8+
<script>
9+
function Product(name) {
10+
this.name = name;
11+
}
12+
console.log(Product.prototype);
13+
// var p1 = new Product('Piyali');
14+
// var p2 = new Product('Test');
15+
// console.log(txt = 'Good Morning ! ' + p1.name);
16+
// console.log(p1.constructor === p2.constructor);
17+
// console.log(p1.constructor.prototype);
18+
// console.log(p2.constructor.prototype.owner);
19+
</script>
20+
</body>
21+
</html>

spread_operator.html

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,133 @@
66
<body>
77
<h2>Spread operator</h2>
88
<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+
14136
</script>
15137
</body>
16138
</html>

0 commit comments

Comments
 (0)