5

Let's say I have two deep objects:

var oldData = {
  "id": 1,
  "first_name": "Eric",
  "last_name": "Henry",
  "info": {
    "email": "ehenry0@smh.com.au",
    "gender": "Male",
    "ip_address": "7.11.169.150",
    "age": 11
  }
};

var newData = {
  "id": 2,
  "first_name": "Tommy",
  "last_name": "Henry",
  "info": {
    "email": "tommy@ghenrry.com",
    "gender": "Male",
    "ip_address": "7.11.169.150",
    "age": 15
  }
};

How would I use lodash (or JavaScript) to traverse through each object and get the value of each different value, so in the top case it would be

[
   {old: 1, new: 2},
   {old: 'Eric', new: 'Tommy'},
   {old: 'ehenry0@smh.com.au', new: 'tommy@ghenrry.com'},
   {old: 11, new: 15},
]

Here is what I have so far:

var oldData = {
  "id": 1,
  "first_name": "Eric",
  "last_name": "Henry",
  "info": {
    "email": "ehenry0@smh.com.au",
    "gender": "Male",
    "ip_address": "7.11.169.150",
    "age": 11
  }
};

var newData = {
  "id": 2,
  "first_name": "Tommy",
  "last_name": "Henry",
  "info": {
    "email": "tommy@ghenrry.com",
    "gender": "Male",
    "ip_address": "7.11.169.150",
    "age": 15
  }
};

var diffObj = _.difference(_.keys(oldData), _.keys(newData));

console.log(JSON.stringify(diffObj, null, 4));
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
red
  • 305
  • 1
  • 4
  • 15
  • 2
    Possible duplicate of [How to do a deep comparison between 2 objects with lodash?](http://stackoverflow.com/questions/31683075/how-to-do-a-deep-comparison-between-2-objects-with-lodash) – Ala Eddine JEBALI Feb 01 '17 at 14:19
  • @AlaEddineJEBALI Those answers do not offer the solution I need help with. 1st answer: not a deep comparison, 2nd answer: isEqual comparison, 3rd answer: does not seem to work. – red Feb 01 '17 at 14:34

2 Answers2

3

The solution using custom recursive function compareUserData and Object.keys() function:

var oldData = { "id": 1, "first_name": "Eric", "last_name": "Henry", "info": { "email": "ehenry0@smh.com.au", "gender": "Male", "ip_address": "7.11.169.150", "age": 11 }
}; 
var newData = { "id": 2, "first_name": "Tommy", "last_name": "Henry", "info": { "email": "tommy@ghenrry.com", "gender": "Male", "ip_address": "7.11.169.150", "age": 15 }
};

function compareUserData(oldData, newData, result) {
    Object.keys(oldData).forEach(function (k) {
        if (typeof oldData[k] !== 'object') {
            if (oldData[k] != newData[k]) this.push({'old': oldData[k], 'new': newData[k]});
        } else {
            compareUserData(oldData[k], newData[k], this);
        }
    }, result);

    return result;
}

var result = compareUserData(oldData, newData, []);
console.log(result);
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

You may want to iterate through your objects properties, and check manually if something changed. To see how you can iterate on properties : Iterate through object properties

EDIT : Your object has nested properties, read this post to see how you can check that too : How do I check if an object has a property in JavaScript?

Community
  • 1
  • 1
PaulDennetiere
  • 179
  • 2
  • 10