Created
November 21, 2022 14:36
-
-
Save ummahusla/cc06f086c40d2030f4a7ed5a09d06a6e to your computer and use it in GitHub Desktop.
JavaScript DYI map, filter, reduce
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
const arr = [1, 2, 3, 4, 1, 2, 3]; | |
const originalMap = arr.map(i => i + 1); | |
const myMap = (arr, cb) => { | |
let temp = []; | |
for(let i = 0; i < arr.length; i++) { | |
temp.push(cb(arr[i])); | |
} | |
return temp; | |
} | |
const myMapTest = myMap(arr, (i => i + 1)); | |
const originalFilter = arr.filter(i => i === 1); | |
const myFilter = (arr, cb) => { | |
let temp = []; | |
for(let i = 0; i < arr.length; i++) { | |
if (cb(arr[i])) temp.push(arr[i]); | |
} | |
return temp; | |
} | |
const myFiltertest = myFilter(arr, (i => i === 1)); | |
const originalReduce = arr.reduce((a, b) => a + b); | |
const myReduce = (arr, cb, init = arr[0]) => { | |
let temp = init; | |
for(let i = 0; i < arr.length; i++) { | |
temp = cb(temp, arr[i]); | |
} | |
return temp; | |
} | |
console.log(myReduce(arr, (a, b) => a * b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment