Skip to content

Instantly share code, notes, and snippets.

@shape-of-myHeart
Created August 17, 2017 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shape-of-myHeart/6487fd9c6167d938ba79fa8b81275bdf to your computer and use it in GitHub Desktop.
Save shape-of-myHeart/6487fd9c6167d938ba79fa8b81275bdf to your computer and use it in GitHub Desktop.
object map function
let keyMap = obj => (callback, formatter) => {
let keys = Object.keys(obj),
result = [];
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
result[typeof formatter === 'function' ? formatter(key) : i] = callback(obj[key], key);
}
return result;
};
keyMap({
a: 1,
b: 2
})(
num => num + 1,
key => "$" + key
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment