Skip to content

Instantly share code, notes, and snippets.

@victory-sokolov
Last active September 9, 2021 09:50
Show Gist options
  • Save victory-sokolov/01bf7e092dc7f442fe01a0ee37e4bc7f to your computer and use it in GitHub Desktop.
Save victory-sokolov/01bf7e092dc7f442fe01a0ee37e4bc7f to your computer and use it in GitHub Desktop.
Union two objects and exclude false values
/**
* Union two objects and exclude false values when merging same keys
* @param left
* @param right
* @returns New combined object
*/
export function unionWithExclusion(left: object, right: object): object {
return [left, right].reduce((prev: any, current) => {
if(current) {
Object.entries(current).map(([key, value]) => {
if (!value) return;
prev[key] =
typeof value === 'object'
? unionWithExclusion(prev[key] || {}, value)
: value;
});
}
return prev;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment