Last active
May 24, 2021 23:29
-
-
Save mageowl/f7d74320e394fc6893528d7ac74df456 to your computer and use it in GitHub Desktop.
Array map on objects.
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
| function omap(object, cb) { | |
| return Object.fromEntries(Object.entries(object).map(cb)); | |
| } | |
| const mappable = { | |
| map(cb) {return omap(this, cb)} | |
| } | |
| const abc = { | |
| a: 1, | |
| b: 2, | |
| c: 3, | |
| d: 4, | |
| e: 5, | |
| // ... | |
| __proto__: mappable | |
| } | |
| abc.map(([key, value]) => [key, value + 10]) //=> {a: 11, b: 12, c: 13, d: 14, e: 15, ...} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment