Skip to content

Instantly share code, notes, and snippets.

@nkint
Created April 2, 2021 15:59
Show Gist options
  • Save nkint/42edcfcfaab1ba10d4c4062f74d13a3a to your computer and use it in GitHub Desktop.
Save nkint/42edcfcfaab1ba10d4c4062f74d13a3a to your computer and use it in GitHub Desktop.
Map Index Values in typescript
export function mapIndexValues<Key extends string | number | symbol, ValueInput, ValueOutput>(
input: Record<Key, ValueInput>,
iteratee: (key: Key, val: ValueInput, index: number) => ValueOutput
): Record<Key, ValueOutput> {
const entries = Object.entries(input).map(([key, value], index) => [
key,
iteratee(key as Key, value as ValueInput, index)
]);
return Object.fromEntries(entries);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment