I'm using lodash for comparing below two objects:
obj1 = {
"_id": "57767",
"Re": {
"PropertyTypes": [
{
"AllocationID": 13870,
"Percentage": null,
"Type": "Hotels",
"Value": null
},
{
"AllocationID": 13867,
"Percentage": null,
"Type": "Industrial",
"Value": null
}
]
}
}
obj2 = {
"_id": "57767",
"Re": {
"PropertyTypes": [
{
"AllocationID": 13867,
"Percentage": null,
"Type": "Industrial",
"Value": null
},
{
"AllocationID": 13870,
"Percentage": null,
"Type": "Hotels",
"Value": null
}
]
}
}
I see that with isEqual(obj1, obj2)
, the comparison fails and with isMatch(obj1, obj2)
, it works fine.
I would like to know if both isEqual
and isMatch
work in exactly the same ways except for the fact that the order of the PropertyTypes
array's elements is ignored by isMatch
in this case. I did not find this info in isMatch's documentation.
They don't work in the same way really, _.isMatch only performs a partial deep comparison while _.isEqual performs a full deep comparison.
So for the example of two simple objects, we get the results below, isMatch returns true since they overlap.
And in your case, the obj1 and obj2 "Re" properties don't have to match at all to get _.isMatch to return true.
let obj1 = {
"_id": "57767",
"Re": {
"PropertyTypes": [
{
"AllocationID": 13870,
"Percentage": null,
"Type": "Hotels",
"Value": null
},
{
"AllocationID": 13867,
"Percentage": null,
"Type": "Industrial",
"Value": null
}
]
}
}
// Remove Re property of obj2 to demonstrate.
let obj2 = {
"_id": "57767",
"Re": {
}
}
console.log("_.isEqual(obj1, obj2)", _.isEqual(obj1, obj2));
console.log("_.isMatch(obj1, obj2)", _.isMatch(obj1, obj2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>