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 952d271

Browse files
author
munnasorder
committed
map function add
1 parent a145b27 commit 952d271

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎map.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const myArray = [1,2,3,4,5,6,7,8,9];
2+
3+
// map example 1
4+
Array.prototype.myMap = function(cb) {
5+
let result = [];
6+
for (let i = 0, length = this.length; i < length; i++) {
7+
result.push(cb(this[i], i, this));
8+
};
9+
return result;
10+
}
11+
12+
// myArray.myMap((doc, i, arr) => console.log(doc));
13+
// output 1 2 3 4 5 6 7 8 9
14+
15+
16+
// map example 2
17+
function myOwnMap(arr, cb) {
18+
let result = [];
19+
for (let i = 0, length = arr.length; i < length; i++) {
20+
result.push(cb(arr[i], i, arr));
21+
};
22+
return result;
23+
}
24+
// myOwnMap(myArray, (doc, i, arr) => console.log(doc));
25+
// output 1 2 3 4 5 6 7 8 9

0 commit comments

Comments
(0)

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