-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayPollyfiils.js
More file actions
53 lines (43 loc) · 1.05 KB
/
ArrayPollyfiils.js
File metadata and controls
53 lines (43 loc) · 1.05 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
const arr = [1, 2, 3, 4, 5, 6];
Array.prototype.myforEach = function (callback) {
for (let i = 0; i < this.length; i++) {
callback(this[i], i, this);
}
};
arr.myforEach((val) => {
console.log(val);
});
arr.map((val) => {
return val * 2;
});
Array.prototype.myMap = function (callback) {
const modifiedArray = new Array(this.length);
for (let i = 0; i < this.length; i++) {
modifiedArray[i] = callback(this[i], i, this);
}
return modifiedArray;
};
arr.filter((val) => {
return val % 2 == 0;
});
Array.prototype.myFilter = function (callback, thisArg) {
let arr = [];
for (let i = 0; i < this.length; i++) {
if (callback.call(thisArg, this[i], i, this)) {
arr.push(this[i]);
}
}
return arr;
};
// arr.reduce((),10)
Array.prototype.myReduce = function (callback, intialValue) {
let value = intialValue;
for (let i = 0; i < this.length; i++) {
value = callback.call(this, value, this[i], i, this);
}
return value;
};
const res = arr.myReduce((prevVal, val) => {
return prevVal + val;
}, 0);
console.log(res);