Last active
August 21, 2020 22:06
-
-
Save gabrielrufino/e41b1161c54cd36c4c6a130ea8346ac6 to your computer and use it in GitHub Desktop.
My implementation of Map, Filter and Reduce <3
This file contains 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
Array.prototype.map = function (callback) { | |
const result = [] | |
for (let i = 0; i < this.length; i++) { | |
result.push(callback(this[i], i, this)) | |
} | |
return result | |
} | |
Array.prototype.filter = function (callback) { | |
const result = [] | |
for (let i = 0; i < this.length; i++) { | |
if (callback(this[i], i, this)) { | |
result.push(this[i]) | |
} | |
} | |
return result | |
} | |
Array.prototype.reduce = function (callback, initial = 0) { | |
let accumulator = initial | |
for (let i = 0; i < this.length; i++) { | |
accumulator = callback(accumulator, this[i], i, this) | |
} | |
return accumulator | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment