Last active
July 19, 2017 20:36
-
-
Save gor181/60dd8f70eb6ef8b469fff1b5f91af8e1 to your computer and use it in GitHub Desktop.
simple reduce and map (from top of my head)
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
const reduce = (array, callback, initialValue) => { | |
let result; | |
for(let i = 0; i < array.length; i++) { | |
result = callback(result || initialValue, array[i], i); | |
} | |
return result; | |
}; | |
const map = (array, callback) => { | |
return reduce(array, (res, item, index) => { | |
res.push(callback(item, index)); | |
return res; | |
}, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment