Skip to content

Instantly share code, notes, and snippets.

@jcampuza
Created April 5, 2021 13:49
Show Gist options
  • Save jcampuza/be032d511aba26b3a8b6eef1063bc3db to your computer and use it in GitHub Desktop.
Save jcampuza/be032d511aba26b3a8b6eef1063bc3db to your computer and use it in GitHub Desktop.
Combine Latest that functions with a keyed object
const combineLatestObj = <T>(
obj: T,
): Observable<{ [K in keyof T]: ObservedValueOf<T[K]> }> => {
const keys = Object.keys(obj);
const values = Object.values(obj);
return combineLatest(values).pipe(
map(
(observedValues) =>
keys.reduce((result, key, i) => {
(result as any)[key] = observedValues[i];
return result;
}, {}) as {
[K in keyof T]: ObservedValueOf<T[K]>;
},
),
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment