Created
April 2, 2021 15:59
-
-
Save nkint/42edcfcfaab1ba10d4c4062f74d13a3a to your computer and use it in GitHub Desktop.
Map Index Values in typescript
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
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