JavaScript (Lodash) - Deep comparison of two objects - Stack Overflow

Let's say I have two deep objects:var oldData = {"id": 1,"first_name": "E

Let's say I have two deep objects:

var oldData = {
  "id": 1,
  "first_name": "Eric",
  "last_name": "Henry",
  "info": {
    "email": "[email protected]",
    "gender": "Male",
    "ip_address": "7.11.169.150",
    "age": 11
  }
};

var newData = {
  "id": 2,
  "first_name": "Tommy",
  "last_name": "Henry",
  "info": {
    "email": "[email protected]",
    "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: '[email protected]', new: '[email protected]'},
   {old: 11, new: 15},
]

Here is what I have so far:

var oldData = {
  "id": 1,
  "first_name": "Eric",
  "last_name": "Henry",
  "info": {
    "email": "[email protected]",
    "gender": "Male",
    "ip_address": "7.11.169.150",
    "age": 11
  }
};

var newData = {
  "id": 2,
  "first_name": "Tommy",
  "last_name": "Henry",
  "info": {
    "email": "[email protected]",
    "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=".17.4/lodash.min.js"></script>

Let's say I have two deep objects:

var oldData = {
  "id": 1,
  "first_name": "Eric",
  "last_name": "Henry",
  "info": {
    "email": "[email protected]",
    "gender": "Male",
    "ip_address": "7.11.169.150",
    "age": 11
  }
};

var newData = {
  "id": 2,
  "first_name": "Tommy",
  "last_name": "Henry",
  "info": {
    "email": "[email protected]",
    "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: '[email protected]', new: '[email protected]'},
   {old: 11, new: 15},
]

Here is what I have so far:

var oldData = {
  "id": 1,
  "first_name": "Eric",
  "last_name": "Henry",
  "info": {
    "email": "[email protected]",
    "gender": "Male",
    "ip_address": "7.11.169.150",
    "age": 11
  }
};

var newData = {
  "id": 2,
  "first_name": "Tommy",
  "last_name": "Henry",
  "info": {
    "email": "[email protected]",
    "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/lodash/4.17.4/lodash.min.js"></script>

Share Improve this question asked Feb 1, 2017 at 14:17 redred 3051 gold badge4 silver badges15 bronze badges 2
  • 2 Possible duplicate of How to do a deep parison between 2 objects with lodash? – Ala Eddine JEBALI Commented Feb 1, 2017 at 14:19
  • @AlaEddineJEBALI Those answers do not offer the solution I need help with. 1st answer: not a deep parison, 2nd answer: isEqual parison, 3rd answer: does not seem to work. – red Commented Feb 1, 2017 at 14:34
Add a ment  | 

2 Answers 2

Reset to default 3

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

var oldData = { "id": 1, "first_name": "Eric", "last_name": "Henry", "info": { "email": "[email protected]", "gender": "Male", "ip_address": "7.11.169.150", "age": 11 }
}; 
var newData = { "id": 2, "first_name": "Tommy", "last_name": "Henry", "info": { "email": "[email protected]", "gender": "Male", "ip_address": "7.11.169.150", "age": 15 }
};

function pareUserData(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 {
            pareUserData(oldData[k], newData[k], this);
        }
    }, result);

    return result;
}

var result = pareUserData(oldData, newData, []);
console.log(result);

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744255606a4565384.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信