Created
September 5, 2019 05:39
-
-
Save lukeggchapman/0070602a85ee1381353c767035fbfa29 to your computer and use it in GitHub Desktop.
Typescript map object
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
// Like array.map, but for objects | |
const mapObj = <U, T extends object>( | |
obj: T, | |
mapFn: (val: T[keyof T], key?: keyof T) => U | |
): { [K in keyof T]: U } => { | |
return Object.assign( | |
{}, | |
...Object.entries(obj).map(([k, v]) => ({ [k]: mapFn(v, k as keyof T) })) | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment