I am comparing two objects with the help of Lodash isEqual function and trying to get difference with difference function.
The difference function is returning whole object instead of only those attributes which are different.
Is there any way to find only mismatched attributes in objects?
const obj1 = {
a: "old value",
b: {
c: "old value"
},
d: 123
}
const obj2 = {
a: "old value",
b: {
c: "new value"
},
d: 321
}
const changes =
_.differenceWith(_.toPairs(obj2), _.toPairs(obj1), _.isEqual)
// Changes in array form
console.log(changes)
// Changes in object form
console.log(_.fromPairs(changes))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With