Last active
July 1, 2020 14:49
-
-
Save greglecki/d55e8ec79b71021ee4c34466b7d764aa to your computer and use it in GitHub Desktop.
Get diff of two js 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
| function getDiff(baseTranslation, targetTranslation) { | |
| var result = {}; | |
| Object.keys(baseTranslation).forEach((key) => { | |
| var obj = targetTranslation[key]; | |
| if (!obj || (typeof(obj) === 'string' && JSON.stringify(obj) === JSON.stringify(baseTranslation[key]))) { | |
| result[key] = baseTranslation[key]; | |
| } else if (typeof(obj) === 'object') { | |
| const o = getDiff(baseTranslation[key], obj); | |
| if (Object.keys(o).length > 0) { | |
| result[key] = o; | |
| } | |
| } | |
| }); | |
| return result; | |
| } | |
| // Then right click on the object -> Store as global variable | |
| // This should give you an variable name like temp1 | |
| // Type in console copy(type1) to copy to the clipboard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment