Skip to content

Instantly share code, notes, and snippets.

@mertenvg
Created January 24, 2019 12:48
Show Gist options
  • Save mertenvg/1ac3b95ca48a5b50755e2ce8aec6d98d to your computer and use it in GitHub Desktop.
Save mertenvg/1ac3b95ca48a5b50755e2ce8aec6d98d to your computer and use it in GitHub Desktop.
Typescript deep merge function
function merge(a: any, b: any): any {
return (a !== Object(a) || b !== Object(b))
? b
: Object.keys(b).filter((k: string) => !a[k]).reduce(
(o: { [i: string]: any }, k: string) => {
o[k] = b[k];
return o;
},
Object.keys(a).reduce(
(o: { [i: string]: any }, k: string) => {
o[k] = b[k]
? merge(a[k], b[k])
: a[k];
return 0;
},
{}
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment