如何使用 lodash 比较对象而不考虑其顺序

Tom*_*sek 7 javascript lodash

我正在尝试使用 lodash 比较两个对象,如下所示。问题是它总是返回 false。我认为问题在于对象具有不同的键和值顺序。然而,无论订单如何,我都找不到如何比较它的解决方案。

\n\n

如何忽略顺序并正确比较两个对象?

\n\n
var obj1 = {                                                                                                                     \n  event: 'pageInformation',                                                                                           \n  page: { type: 'event', category: 'sportsbook' },                                                                     \n  username: 'anonymous',                                                                                              \n  pagePath: '/',                \n  item: { name: 'Barcelona - Leganes', id: '123' },                                                           \n  contest: { name: '1.\xc5\xa0panielsko', id: 'MSK70' },                                                                     \n  category: { name: 'Futbal', id: 'MSK3' },                                                                           \n  teams: [                                                                                                            \n    { id: 'barcelona', name: 'Barcelona' },                                                                           \n    { id: 'leganes', name: 'Leganes' }                                                                                \n  ]                                                                                                                   \n}                                                                                                                     \nvar obj2 = {                                                                                                                     \n  event: 'pageInformation',                                                                                           \n  page: { type: 'event', category: 'sportsbook' },                                                                    \n  username: 'anonymous',                                                                                              \n  pagePath: '/',                \n  category: { id: 'MSK3', name: 'Futbal' },                                                                           \n  contest: { name: '1.\xc5\xa0panielsko', id: 'MSK70' },                                                                     \n  item: { id: '123', name: 'Barcelona - Leganes' },                                                           \n  teams: [                                                                                                            \n    { name: 'Barcelona', id: 'barcelona' },                                                                           \n    { name: 'Leganes', id: 'leganes' }                                                                                \n  ]                                                                                                                   \n}         \n\nfunction compareObjects(obj1, obj2){\n return _.isMatch(obj1, obj2);\n}   \n
Run Code Online (Sandbox Code Playgroud)\n

Dyl*_*ler 6

您可以使用 isEqual 函数来执行深度相等检查(无论键顺序如何):

   _.isEqual(obj1, obj2)
Run Code Online (Sandbox Code Playgroud)

在此处查看更多信息:https ://lodash.com/docs/2.4.2#isEqual