Created
April 13, 2020 06:32
-
-
Save batpad/c40137861e391996f677656036b0cdf6 to your computer and use it in GitHub Desktop.
This file contains 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
function difference(obj1, obj2) { | |
const ret = { | |
new: {}, | |
removed: {}, | |
diff: {} | |
} | |
const keys1 = Object.keys(obj1); | |
const keys2 = Object.keys(obj2); | |
for (let k of keys1) { | |
if (keys2.indexOf(k) === -1) { | |
ret.removed[k] = obj1[k] | |
} | |
const v1 = JSON.stringify(obj1[k]); | |
const v2 = JSON.stringify(obj2[k]); | |
if (v1 !== v2) { | |
ret.diff[k] = { | |
'old': obj1[k], | |
'new': obj2[k] | |
} | |
} | |
} | |
for (let k of keys2) { | |
if (keys1.indexOf(k) === -1) { | |
ret.new[k] = obj2[k]; | |
} | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment