Created
August 17, 2017 16:53
-
-
Save shape-of-myHeart/6487fd9c6167d938ba79fa8b81275bdf to your computer and use it in GitHub Desktop.
object map function
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
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