Created
October 14, 2016 06:34
-
-
Save ZhihaoLau/78b007a2c6216a694fb14da6e80cf5b7 to your computer and use it in GitHub Desktop.
fakeMap in Array@reduce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = [1, 2, 3, 4]; | |
function handleNext(value) { | |
return value * 2 | |
} | |
Array.prototype.fakeMap = function(cb) { | |
return this.reduce((accumulator, next, index) => { | |
accumulator.push(cb(next)) | |
return accumulator; | |
}, []); | |
} | |
var mappedArr = arr.map(handleNext); | |
var reducedArr = arr.fakeMap(handleNext); | |
console.log(reducedArr); | |
console.log(mappedArr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment