Skip to content

Instantly share code, notes, and snippets.

@mudssrali
Last active January 21, 2023 09:43
Show Gist options
  • Save mudssrali/175ddc89cf3fc93c69afbdbf40965057 to your computer and use it in GitHub Desktop.
Save mudssrali/175ddc89cf3fc93c69afbdbf40965057 to your computer and use it in GitHub Desktop.
Deep difference between two object, using lodash
//@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