Skip to content

Instantly share code, notes, and snippets.

@jdanyow
Created May 19, 2021 18:27
Show Gist options
  • Save jdanyow/255dd11b6d0efcec6e9885f8b5333d21 to your computer and use it in GitHub Desktop.
Save jdanyow/255dd11b6d0efcec6e9885f8b5333d21 to your computer and use it in GitHub Desktop.
/**
* Convert an iterable to an object.
*/
export function toObject<
TKey extends string | number | symbol,
TValue
>(iterable: {
[Symbol.iterator](): IterableIterator<[TKey, TValue]>;
}): Record<TKey, TValue> {
const obj = Object.create(null) as Record<TKey, TValue>;
for (const [key, value] of iterable) {
obj[key] = value;
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment