forked from munnasorder/JavaScript_Simple_Problem_Solving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
25 lines (21 loc) · 612 Bytes
/
map.js
File metadata and controls
25 lines (21 loc) · 612 Bytes
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
const myArray = [1,2,3,4,5,6,7,8,9];
// map example 1
Array.prototype.myMap = function(cb) {
let result = [];
for (let i = 0, length = this.length; i < length; i++) {
result.push(cb(this[i], i, this));
};
return result;
}
// myArray.myMap((doc, i, arr) => console.log(doc));
// output 1 2 3 4 5 6 7 8 9
// map example 2
function myOwnMap(arr, cb) {
let result = [];
for (let i = 0, length = arr.length; i < length; i++) {
result.push(cb(arr[i], i, arr));
};
return result;
}
// myOwnMap(myArray, (doc, i, arr) => console.log(doc));
// output 1 2 3 4 5 6 7 8 9