Last active
September 9, 2021 09:50
-
-
Save victory-sokolov/01bf7e092dc7f442fe01a0ee37e4bc7f to your computer and use it in GitHub Desktop.
Union two objects and exclude false values
This file contains hidden or 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
/** | |
* 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