JavaScript Compare two JSON objects and just return another JSON object with only the changes compare json objects java,compare json objects javascript,compare json

You can try following as a simple comparison:

Note: This answer assumes the structure to be exactly same. You can take this as reference and make it more generic for cases where structures are different.

Points to consider:

  • Number of items are different. o1.details have 2 entries and o2.details have say 5 entries.
  • Object structure is different. Properties in both object are different.

function getDifference(o1, o2) {
  var diff = {};
  var tmp = null;
  if (JSON.stringify(o1) === JSON.stringify(o2)) return;

  for (var k in o1) {
    if (Array.isArray(o1[k]) && Array.isArray(o2[k])) {
      tmp = o1[k].reduce(function(p, c, i) {
        var _t = getDifference(c, o2[k][i]);
        if (_t)
          p.push(_t);
        return p;
      }, []);
      if (Object.keys(tmp).length > 0)
        diff[k] = tmp;
    } else if (typeof(o1[k]) === "object" && typeof(o2[k]) === "object") {
      tmp = getDifference(o1[k], o2[k]);
      if (tmp && Object.keys(tmp) > 0)
        diff[k] = tmp;
    } else if (o1[k] !== o2[k]) {
      diff[k] = o2[k]
    }
  }
  return diff;
}

var o1={id:"1",details:[{name:"Peter",address:"Arizona",phone:9900998899},{name:"Jam",address:"Kentucky",phone:56034033343}],profession:"Business"},
o2={id:"2",details:[{name:"Peter",address:"Arizona",phone:9900998899},{name:"David",address:"Boston",phone:434323434}],profession:"Business"};

var d = getDifference(o1, o2)
console.log(d)
Answer:1

I'm learning Angular 2. I'm using a LocationService with an Observable that hands me the coordinates after some time. This is my code. location.service.ts public getLocation(): Observable<any> ...

I'm learning Angular 2. I'm using a LocationService with an Observable that hands me the coordinates after some time. This is my code. location.service.ts public getLocation(): Observable<any> ...

  1. angular subscribe when to unsubscribe
  2. angular subscribe when done
  3. angular subscribe when
  4. angular subscribe when all
  5. angular subscribe when complete
  6. angular when subscribe finished

I'm new to js/programming and just making a simple calculator using prompt. I'm trying to validate that what's been entered are numbers and not strings. I tried this var operatorType = prompt("Do ...

I'm new to js/programming and just making a simple calculator using prompt. I'm trying to validate that what's been entered are numbers and not strings. I tried this var operatorType = prompt("Do ...

  1. if/else statement java
  2. if/else statement python
  3. if/else statement c++
  4. if/else statement javascript
  5. if/else statement code.org
  6. if/else statement r
  7. if/else statement excel
  8. if else statement
  9. if else statement in c
  10. if else statement java
  11. if else statement python
  12. if else statement in c example
  13. if else statement excel
  14. if else statement in r
  15. if else statement javascript
  16. if else statement example
  17. if else statement matlab
  18. if else statement in sql
  19. if else statement java exercises
  20. if else statement arduino

I have an AngulaarJS application where users select the day they want to view and it prints on the calendar the day and the next 30 days. The intended functionality needs to be so that when a user ...

I have an AngulaarJS application where users select the day they want to view and it prints on the calendar the day and the next 30 days. The intended functionality needs to be so that when a user ...

  1. how to get current date in angularjs
  2. angularjs add days to current date

I would like to get a list of all the properties, styles, events and methods of an HTML element. Is there a reflection API in the browsers? I'm looking for something like this: var button = new ...

I would like to get a list of all the properties, styles, events and methods of an HTML element. Is there a reflection API in the browsers? I'm looking for something like this: var button = new ...

  1. does javascript run in browser or server
  2. does javascript run in browser