SHARE
TWEET

Untitled

a guest Jun 20th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. JSON A:
  2. {
  3. "Field A":"1",
  4. "Field B":"2",
  5. "Field D":"Something",
  6. "Field E":"6"
  7. }
  8.  
  9. JSON B:
  10. {
  11. "Field A":"1",
  12. "Field B":"2",
  13. "Field C":"3",
  14. "Field D":"Different"
  15. }
  16.      
  17. {
  18. "Field C":"3", // because function sees jsonstringB had no "Field C"
  19. "Field D": "Different" // sees jsonstringB had a different value for "Field D"
  20. }
  21.      
  22. var compareJSON = function(obj1, obj2) {
  23.   var ret = {};
  24.   for(var i in obj2) {
  25.     if(!obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
  26.       ret[i] = obj2[i];
  27.     }
  28.   }
  29.   return ret;
  30. };
  31.      
  32. isEmpty
  33.      
  34. var compareObj = function(obj1, obj2) {
  35.   var ret = {},rett;
  36.   for(var i in obj2) {
  37.       rett = {};  
  38.       if (typeof obj2[i] === 'object'){
  39.           rett = compareObj (obj1[i], obj2[i]) ;
  40.           if (!isEmpty(rett) ){
  41.            ret[i]= rett
  42.           }              
  43.        }else{
  44.            if(!obj1 || !obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
  45.               ret[i] = obj2[i];
  46.       }
  47.    }
  48.   }
  49.   return ret;
  50. };
  51.      
  52. var compareJSON = function(obj1, obj2) {
  53.       var ret = {};
  54.       for(var i in obj2) {
  55.         if(!obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
  56.           if(!Array.isArray(obj2[i]) || !(JSON.stringify(obj2[i]) == JSON.stringify(obj1[i]))){
  57.           ret[i] = obj2[i];
  58.           }
  59.         }
  60.       }
  61.       return ret;
  62.     };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top