最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to compare objects using lodash regardless on their order - Stack Overflow

programmeradmin3浏览0评论

I am trying to pare 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 pare it regardless on the order.

How to ignore the order and pare 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 pareObjects(obj1, obj2){
 return _.isMatch(obj1, obj2);
}   

I am trying to pare 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 pare it regardless on the order.

How to ignore the order and pare 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 pareObjects(obj1, obj2){
 return _.isMatch(obj1, obj2);
}   
Share Improve this question edited Jun 16, 2020 at 12:25 Tomáš Nosek asked Jun 16, 2020 at 9:41 Tomáš NosekTomáš Nosek 3532 gold badges3 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You can use the isEqual function which does a deep equal check (regardless of key order):

   _.isEqual(obj1, obj2)

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

发布评论

评论列表(0)

  1. 暂无评论