Last active
September 22, 2019 06:05
-
-
Save ermauliks/9e4703f91ab59ed141e17d28a2b10f09 to your computer and use it in GitHub Desktop.
Implementation of Map and Filter Array function in JavaScript
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.myMap = function(fn) { | |
let newArray = []; | |
this.forEach(item => { | |
newArray.push(fn(item)); | |
}) | |
return newArray; | |
} | |
Array.prototype.myFilter = function(fn) { | |
let newArray = []; | |
this.forEach(item => { | |
if (fn(item)) | |
newArray.push(item); | |
}) | |
return newArray; | |
} | |
let map = [1, 2, 3, 4, 5]; | |
const sqrt = (a) => a * a; | |
const gt2 = (a) => a > 2; | |
console.log(map.myMap(sqrt)); | |
console.log(map.myFilter(gt2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment