Last active
June 28, 2018 13:44
-
-
Save MKRhere/b59c2d6f3e91e1ccd54a60e80c781f0f to your computer and use it in GitHub Desktop.
Random JavaScript utils
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
// Object map | |
function mapobjIndexed (fn, obj) { | |
const curriedMapobjIndexed = obj => Object.keys(obj).reduce((acc, cur) => ({...acc, [cur] : fn(obj[cur], cur, obj) }), {}) | |
if(arguments.length < 2) return curriedMapobjIndexed; | |
return curriedMapobjIndexed(obj); | |
} | |
// Prototype version | |
Object.prototype.map = function (fn) { | |
return Object.keys(this).reduce( | |
(acc, cur) => ({...acc, [cur] : fn(this[cur], cur, this) }), | |
{} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment