-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathPolyfillOfarrayMethod.js
More file actions
338 lines (259 loc) · 6.92 KB
/
PolyfillOfarrayMethod.js
File metadata and controls
338 lines (259 loc) · 6.92 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Advanced Javascript
// const object1 = {
// name: 'adam',
// lastName: "gilchrist",
// printName: function () {
// console.log("hey" ,this.name ,"hey" ,this.lastName)
// }
// }
// const object2 = {
// home: "bnaxv",
// adress: 'hvs gx'
// }
// object2.__proto__ = object1;
// console.log('after assigning proto')
// object2.printName();
// console.log(object2.lastName)
// console.log(object2.name)
// console.log(object1 , "Hye fine na")
// console.log(object2.valueOf() , "Hye fine na")
// // what can be the value of prototype => object can be null
// const p1company = {
// name: "Witviper",
// GSTNO: 'GZXTPRETZ',
// set CompanyName(name) {
// this.name = name
// } ,
// get CompanyName () {
// return this.name + ' ' + this.GSTNO
// }
// }
// const p2company = {
// __proto__: p1company,
// isChild: true,
// }
// const grandChild = {
// __proto__: p2company,
// }
// console.log(grandChild.name)
// grandChild.CompanyName = "Chilta pvt ltd."
// console.log(grandChild.name) //
// console.log(p1company.name)
// // will the context of this will be changed on the protoype => No
// //
// const obj1 = {
// 1: "Hey ",
// 2: 'Hello',
// 3: 'Good morning'
// }
// const obj2 = {
// }
// const obj3 = {
// }
// const obj4 = {
// }
// Object.prototype.print = function () {
// console.log('Print me')
// }
// const obj5 = {
// }
// obj5.print();
// obj4.print();
// obj3.print();
// obj2.print();
// obj1.print();
// console.log(obj4)
// const ob6 ={
// }
// ob6.print();
// // findAverageofGivernArray
// const array123 = [3,4,5,6,7];
// // console.log(array123.average());
// Array.prototype.average = function(){
// let sum = 0;
// for(let i=0;i<this.length;i++) {
// sum = sum+this[i];
// }
// return sum/this.length
// }
// const avg = array123.average();
// console.log(avg)
// const avg2=[3,4,5,6,7,78].average();
// console.log(avg2)
// console.log([])
// String.prototype.stylisPrint = function() {
// console.log('i am stylish print' , this)
// }
// const message = 'Hey we are learning prototype';
// message.stylisPrint()
// // we have mutiple browser
// // some browser does not support new features of javascript
// // we need to create our own functions of features on that scenarion
// // that is called the pollyfill
// // creating your map filter reduce forEACH , BIND , CALL , APPLY
// // FLATTEN ARRAY
// Array.prototype.includesOneOf = function (list) {
// if(list.length === 0 || this.length===0) return false;
// for(let i=0;i<this.length;i++) {
// if(Array.isArray(list[0]) && Array.isArray(this[0])) {
// for(let j=0;j<list.length;j++) {
// const che = this[i];
// const che1 = list[j];
// if(che.length === che1.length && che.every((item ,index)=> {return item == che1[index]})) {
// return true
// }
// }
// }
// // else if(typeof this[0] === "object" && typeof list[0]){
// // }
// else if(list[0].constructor == 'Object' && list[0].constructor == 'Object')
// else {
// if(list.includes(this[i])) {
// return true;
// }
// }
// }
// return false;
// }
// const arr1 = [1,2,3,4];
// const arr2 = [3,4,5,6];
// const arr3 = ["hello","hello2","checking","perfect"];
// const arr4 = ["hello","perfect2"];
// const res = arr1.includesOneOf(arr2);
// console.log(res)
// const res1 = arr4.includesOneOf(arr3);
// console.log(res1)
// // array ??
// // object ??
// const arrr3 = [[12,3,4] , [12,4,5]]
// const arrr4 = [[12,3,6] , [12,4,5] , [1,2,3]]
// // [[Protoype]]
// for ()
// forEach()
// while()
// map
// filter
// reduce
// for loop
// const mutliply = (array , i)=> {
// array[i] = array[i]*2;
// }
// imperative approache
const array = [1, 2, 3, 4];
// for(let i=0 ;i<array.length;i*2+1) {
// array[i]= array[i]*2;
// }
const callBackFunc = (currElement, index, array) => {
console.log(currElement, index, array);
};
array.forEach((currElement, index, orArray) => {
orArray[index] = orArray[index] * 2;
console.log(currElement, index, array);
});
// Map
const adda = () => {};
console.log("Heyy", adda());
const array1 = [3, 4, 5, 6, 7];
const callBackFuncMap = (element, index) => {
return element * 2;
};
const array2 = array1.map(callBackFuncMap);
console.log(array2, array1);
Array.prototype.myMap = function (callbackFunc) {
let newArray = [];
for (let i = 0; i < this.length; i++) {
const updatedElement = callbackFunc(this[i], i, this);
newArray.push(updatedElement);
}
return newArray;
};
const array4 = [2, 3, 4, 5, 6, 7];
const checkCall = (elem, index, array) => {
return {
[index]: elem,
};
};
const data1 = array4.myMap(checkCall);
const data2 = array4.map(checkCall);
console.log("data1", data1);
console.log(JSON.stringify(data1) === JSON.stringify(data2));
// filter own pollyfill
Array.prototype.myFilter = function (callbackFunc) {
let newArray = [];
for (let i = 0; i < this.length; i++) {
const updatedElement = callbackFunc(this[i], i, this);
if (updatedElement) {
newArray.push(this[i]);
}
}
return newArray;
};
Array.prototype.myMap = function (callbackFunc) {
let newArray = [];
for (let i = 0; i < this.length; i++) {
const updatedElement = callbackFunc(this[i], i, this);
newArray.push(this[i]);
}
return newArray;
};
const data123 = [4, 5, 6, 7, 8, 8, 8, 8, 9, 8, 8, 8];
let ans =
Array.isArray(data123) &&
data123.filter((ele) => {
return ele < 8;
});
// array reduce polyfill
const array5 = [3, 7, 9];
const callbackReduce = (prev, curr, index) => {
if (curr % 2 == 0) {
prev = curr;
}
};
// call , bind , apply
// reduce
// questions
// prototype
// protoype
// class
// async
// sat and sun
// polffill => map , filter and reduce
// Reduce Polyfill
const totalSum = array1.reduce((prev, curr) => {
prev = prev + curr;
return prev;
}, 0);
Array.prototype.myReduce = function (callbackFunction, initial) {
this.forEach((elem) => {
initial =
typeof initial !== "undefined" ? callbackFunction(initial, elem) : elem;
});
return initial;
};
// it is manadatory key in reduce as initial value
const sumArray = [3, 4, 5];
const sum = sumArray.myReduce((prev, curr) => {
return prev + curr;
});
console.log("sum=> ", sum);
const sum2 = sumArray.reduce((prev, curr) => {
return prev * curr;
}, 0);
console.log(sum2);
Array.prototype.myReduce1 = function (cb, initialValue) {
let ans = initialValue || (this[0] ? this[0] : undefined);
if (ans !== undefined) {
for (let i = initialValue ? 0 : 1; i < this.length; i++) {
ans = cb(ans, this[i], i, this);
}
}
return ans;
};
const data = [].myReduce1((acc, value) => {
return acc + value;
});
const data22 = [].reduce((acc, value) => {
return acc + value;
});
console.log(data1);
console.log(data2);