Skip to content

Instantly share code, notes, and snippets.

@MKRhere
Last active June 28, 2018 13:44
Show Gist options
  • Save MKRhere/b59c2d6f3e91e1ccd54a60e80c781f0f to your computer and use it in GitHub Desktop.
Save MKRhere/b59c2d6f3e91e1ccd54a60e80c781f0f to your computer and use it in GitHub Desktop.
Random JavaScript utils
// 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