[英]How to compare objects using lodash regardless on their order
I am trying to compare two objects using lodash like below.我正在尝试使用 lodash 比较两个对象,如下所示。 The problem is that it always returns false.
问题是它总是返回false。 I think that the issue is that the objects have different order of keys and values.
我认为问题在于对象具有不同的键和值顺序。 I however couldn't find a solution on how to compare it regardless on the order.
但是,无论订单如何,我都找不到如何比较它的解决方案。
How to ignore the order and compare the two objects correctly?如何忽略顺序并正确比较两个对象?
var obj1 = {
event: 'pageInformation',
page: { type: 'event', category: 'sportsbook' },
username: 'anonymous',
pagePath: '/',
item: { name: 'Barcelona - Leganes', id: '123' },
contest: { name: '1.Španielsko', id: 'MSK70' },
category: { name: 'Futbal', id: 'MSK3' },
teams: [
{ id: 'barcelona', name: 'Barcelona' },
{ id: 'leganes', name: 'Leganes' }
]
}
var obj2 = {
event: 'pageInformation',
page: { type: 'event', category: 'sportsbook' },
username: 'anonymous',
pagePath: '/',
category: { id: 'MSK3', name: 'Futbal' },
contest: { name: '1.Španielsko', id: 'MSK70' },
item: { id: '123', name: 'Barcelona - Leganes' },
teams: [
{ name: 'Barcelona', id: 'barcelona' },
{ name: 'Leganes', id: 'leganes' }
]
}
function compareObjects(obj1, obj2){
return _.isMatch(obj1, obj2);
}
You can use the isEqual function which does a deep equal check (regardless of key order):您可以使用 isEqual function 进行深度相等检查(无论密钥顺序如何):
_.isEqual(obj1, obj2)
See more here: https://lodash.com/docs/2.4.2#isEqual在此处查看更多信息: https://lodash.com/docs/2.4.2#isEqual
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.