简体   繁体   中英

How to compare objects using lodash regardless on their order

I am trying to compare two objects using lodash like below. The problem is that it always returns 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(obj1, obj2)

See more here: https://lodash.com/docs/2.4.2#isEqual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM