3

Recently I am working on the project. I am facing the problem in getting the difference between two JavaScript objects. Below are two examples of the object. The Second Object has one extra key. So how to get the difference between the objects.

Example 1

{
  "title": "JavaScript Tutorial",
  "body":  "JavaScript",
  "comments": [ 
    {
      "name":    "John Smith",
      "comment": "Great article",
      "age":     28,
      "stars":   4,
      "date":    "2014-09-01"
    },

  ]
}

Example 2

{
  "title": "JavaScript tutorial",
  "body":  "JavaScript.",
  "topics":"how learn JavaScript withing 10 days",
  "comments": [ 
    {
      "name":    "John Smith",
      "comment": "Great article",
      "age":     28,
      "stars":   4,
      "date":    "2014-09-01"
    },

  ]
}

I want result like below. Below is key different in two object

"topics":"how learn JavaScript withing 10 days",

Kunal Ingole
  • 93
  • 1
  • 7

2 Answers2

0

Have a look at deep-diff which you can essentially use like this for your sample data

var diff = require('deep-diff').diff;

var lhs = {
  "title": "JavaScript Tutorial",
  "body":  "JavaScript",
  "comments": [ 
    {
      "name":    "John Smith",
      "comment": "Great article",
      "age":     28,
      "stars":   4,
      "date":    "2014-09-01"
    },

  ]
};

var rhs = {
  "title": "JavaScript tutorial",
  "body":  "JavaScript.",
  "topics":"how learn JavaScript withing 10 days",
  "comments": [ 
    {
      "name":    "John Smith",
      "comment": "Great article",
      "age":     28,
      "stars":   4,
      "date":    "2014-09-01"
    },

  ]
};

var differences = diff(lhs, rhs);
console.log(differences);

which will output

[ DiffEdit {
    kind: 'E',
    path: [ 'title' ],
    lhs: 'JavaScript Tutorial',
    rhs: 'JavaScript tutorial' },
  DiffEdit {
    kind: 'E',
    path: [ 'body' ],
    lhs: 'JavaScript',
    rhs: 'JavaScript.' },
  DiffNew {
    kind: 'N',
    path: [ 'topics' ],
    rhs: 'how learn JavaScript withing 10 days' } ]

As you can see, there are more changes than only the new property. The kind of difference is also indicated by the kind property. If you only need the new property then just consider the objects with kind === 'N'

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
0

If recursion is not a factor you can just loop over the properties of both checking if both possess the corresponding property.

Make sure you loop over both respectively, that way you don't get any false positives.

try this code out

function sameKeys (obj1, obj2) {
    let diffs = []

    for (let prop in obj1) {
        if (undefined === typeof obj2[prop]
            || obj2[prop] !== obj1[prop]) {

            diffs.push({
                type: (obj2[prop] !== undefined ? "Not equal" : "Undefined"),
                field: prop,
                whichOne: "Object 2",
                values: {
                    object1: obj1[prop],
                    object2: obj2[prop]
                }
            })
        }
    }

    for (let prop in obj2) {
        if (undefined === typeof obj1[prop]
            || obj1[prop] !== obj2[prop]) {

            diffs.push({
                type: (obj1[prop] !== undefined ? "Not equal" : "Undefined"),
                field: prop,
                whichOne: "Object 1",
                values: {
                    object1: obj1[prop],
                    object2: obj2[prop]
                }
            })
        }
    }

    return diffs
}
  • `!obj2[prop]` will not give correct results if the property exists but has a falsey value. – RobG Jul 28 '16 at 07:19
  • It will still not work correctly if *obj1[prop]* has the value *undefined*. Better to use a *hasOwnProperty* check or use *Object.keys* to check own properties. *for..in* also iterates over inherited enumerable properties. – RobG Aug 01 '16 at 03:59
  • I check this code but it's not getting different between two objects. Return the wrong result if objects is nested – Kunal Ingole Aug 02 '16 at 12:27