Last active
January 21, 2023 09:43
-
-
Save mudssrali/175ddc89cf3fc93c69afbdbf40965057 to your computer and use it in GitHub Desktop.
Deep difference between two object, using lodash
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
//@ts-nocheck | |
import { isEqual, isObject, transform } from 'lodash' | |
/** | |
* Deep diff between two object, using lodash | |
* @param {Object} object Object compared | |
* @param {Object} base Object to compare with | |
* @return {Object} Return a new object who represent the diff | |
*/ | |
export function difference<T>(object: T, base: T): T { | |
if (base === undefined) { | |
return object | |
} | |
return transform(object, (result, value, key) => { | |
if (!isEqual(value, base[key])) { | |
result[key] = | |
isObject(value) && isObject(base[key]) ? difference(value, base[key]) : value | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment