Lodash: How to do an Unordered comparison between two Objects/Arrays ?

Created on 10 Mar 2016  路  3Comments  路  Source: lodash/lodash

Hello,

I was wondering how can we do an Unordered comparison between two complex entities.

In exemple :

var o1 = {
    id: 1,
    children: [{ a: 1, b: 2 },  { a: 3, b: 4}]
};
var o2 = {
    id: 1,
    children: [{ a: 3, b: 4 },  { a: 1, b: 2}]
};

_.isEqual(o1, o2, UNORDERED_COMPARE_FLAG);
// Should return true.

// Same should apply to these simple objects
var o1 = {id: 1, a: 1};
var o2 = {a: 1, id: 1};

_.isEqual(o1, o2, UNORDERED_COMPARE_FLAG);
// This should return true.

These two arrays are definitely equals to each other in term of properties/value the only difference is the order of the objects/arrays.

Actually, the _.isEqual() or _.isEqualWith() functions doesn't seem to use this kind of behaviour.
But after looking a bit on the code, i've saw some interesting stuffs in there ! Like the baseIsEqualDeep() function who use the bitmask param.
Why this options isn't exposed to _.isEqual() or _.isEqualWith() functions ?

Thank you.

question

Most helpful comment

You can do something like _.isMatch(o1, o2) which performs an unordered partial match. The downside being it's a partial match so a little more forgiving. To get what you want you can combo _.isEqualWith + _.isMatch.

All 3 comments

You can do something like _.isMatch(o1, o2) which performs an unordered partial match. The downside being it's a partial match so a little more forgiving. To get what you want you can combo _.isEqualWith + _.isMatch.

Ho thanks ! This is exactly what i was looking for ! :-)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

toranb picture toranb  路  3Comments

satrong picture satrong  路  3Comments

jasonkarns picture jasonkarns  路  3Comments

bryandowning picture bryandowning  路  3Comments

rtheunissen picture rtheunissen  路  3Comments