Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lukeggchapman
Created September 5, 2019 05:39
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 lukeggchapman/0070602a85ee1381353c767035fbfa29 to your computer and use it in GitHub Desktop.
Save lukeggchapman/0070602a85ee1381353c767035fbfa29 to your computer and use it in GitHub Desktop.
Typescript map object
// 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