Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0432dc0

Browse files
author
Swastikyadav
committed
Add polyfill for map, filter, and reduce array methods
1 parent 79f16c2 commit 0432dc0

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎Polyfills/arrayMethods.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Polyfill of Array.prototype.map method
2+
Array.prototype.myMap = function (callback) {
3+
const resultArr = [];
4+
5+
for(let i = 0; i < this.length; i++) {
6+
resultArr.push(callback(this[i], i, this));
7+
}
8+
9+
return resultArr;
10+
};
11+
12+
// Polyfill of Array.prototype.filter method
13+
Array.prototype.myFilter = function (callback) {
14+
const resultArr = [];
15+
16+
for(let i = 0; i < this.length; i++) {
17+
const bool = callback(this[i], i, this);
18+
bool === true && resultArr.push(this[i]);
19+
}
20+
21+
return resultArr;
22+
};
23+
24+
// Polyfill of Array.prototype.reduce method
25+
Array.prototype.myReduce = function (callback, initialValue) {
26+
let acc = initialValue;
27+
for(let i = 0; i < this.length; i++) {
28+
if (i === 0 && initialValue === undefined) {
29+
acc = this[i];
30+
} else {
31+
acc = callback(acc, this[i], i, this);
32+
}
33+
}
34+
35+
return acc;
36+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /