Created
October 11, 2018 17:01
-
-
Save sethdavis512/58fef8dc861d5c1fa6216300b723a324 to your computer and use it in GitHub Desktop.
Lodash Diff Objects
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
import { transform, isEqual, isObject } 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 | |
*/ | |
function difference(object, base) { | |
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