Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
179
rated 0 times [  184] [ 5]  / answers: 1 / hits: 5988  / 4 Years ago, tue, june 16, 2020, 12:00:00

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);
}

More From » lodash

 Answers
4

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


[#3471] Friday, June 12, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarkulisesa

Total Points: 422
Total Questions: 93
Total Answers: 112

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
clarkulisesa questions
Mon, Feb 24, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
Wed, Mar 27, 19, 00:00, 5 Years ago
;