# Deep equal in any order

**URL:** <https://community.postman.com/t/deep-equal-in-any-order/11509>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [March 24, 2020, 10:17pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509 "2020-03-24T22:17:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrorke](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jrorke](https://community.postman.com/u/jrorke)\
**Post date:** [March 24, 2020, 10:17pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/1 "2020-03-24T22:17:42Z")

</div>

Any plans to support the deep-equal-in-any-order chai plugin in test scripts?  
[https://www.chaijs.com/plugins/deep-equal-in-any-order/](https://www.chaijs.com/plugins/deep-equal-in-any-order/)

… if not, is there an alternative? I tried loading the plugin dynamically, via environment variable, but couldn’t seem to make it work.

---

<div class="post-metadata">

**Author:** ![MariDubois](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@MariDubois](https://community.postman.com/u/MariDubois)\
**Post date:** [August 11, 2020, 9:54am UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/2 "2020-08-11T09:54:18Z")

</div>

I agree this is needed in Postman. Is there any plan to integrate?

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 11, 2020, 11:42am UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/3 "2020-08-11T11:42:06Z")

</div>

lodash is included in the postman sandbox. You could do one of two things:

1. Sort both arrays and use isEqual

```auto
pm.expect(_.isEqual(_.sortBy(arr1), _.sortBy(arr2))).to.be.true;

```

1. Use xor from lodash

```auto
pm.expect(_.xor(arr1, arr2).length).to.equal(0);

```

[Stack Overflow reference](https://stackoverflow.com/questions/29951293/using-lodash-to-compare-arrays-items-existence-without-order)

---

<div class="post-metadata">

**Author:** ![MariDubois](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@MariDubois](https://community.postman.com/u/MariDubois)\
**Post date:** [August 11, 2020, 12:20pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/4 "2020-08-11T12:20:09Z")

</div>

@allen.helton  
Hello

If the array contains objects the sorting does not work fine. Unfortunatly I tried your suggestion before but does not work for nested objects.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 11, 2020, 12:27pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/5 "2020-08-11T12:27:39Z")

</div>

What about using xor?

---

<div class="post-metadata">

**Author:** ![MariDubois](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@MariDubois](https://community.postman.com/u/MariDubois)\
**Post date:** [August 11, 2020, 12:36pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/6 "2020-08-11T12:36:39Z")

</div>

> [@allenheltondev](#):
>
> `_.xor(arr1, arr2).length`

@allenheltondev  
Hello,  
Thank you for your help. Unfortunately doesn’t work neither

```auto
json1 = {
  "a": 1,
  "b": {"toto" : "toto",
    "array" : [
    {"old": {
      "value" :1
    }},
    {"new": {
      "value" :2
    }}
  ]
}
}

json2 = {
  "a": 1,
  "b": {"toto" : "toto",
    "array" : [
   {"new": {
      "value" :2
    }},
     {"old": {
      "value" :1
    }}
  ]
}
}

json3 = {
    "b": {"toto" : "toto",
    "array" : [
    {"old": {
      "value" :1
    }},
    {"new": {
      "value" :2,
      "other" : 4
    }}
  ]
},
  "a": 1

}

function customizer(baseValue, value) {
    
      if (Array.isArray(baseValue) && Array.isArray(value)) {
              return lodash.isEqual(lodash.sortBy(baseValue), lodash.sortBy(value))
            }
        
      }

console.log(lodash.isEqualWith(json1,json2,customizer)) -> false (I expected true as only array order changes)
console.log(lodash.isEqual((lodash.sortBy(json1),lodash.sortBy(json2)))) -> same as above
console.log(lodash.xor(json1, json2).length) -> 0
console.log(lodash.xor(json1, json3).length) -> 0 (I expected different than 0 because of "other" in json3)

```

Thank you for your assistance. Any idea?

Best regards

---

<div class="post-metadata">

**Author:** ![MariDubois](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@MariDubois](https://community.postman.com/u/MariDubois)\
**Post date:** [August 11, 2020, 12:44pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/7 "2020-08-11T12:44:09Z")

</div>

PS : I have tried as well the isMatch however it’s too permissive. I need to have same length in arrays (even nested ones), and I need to have same fields in both json (isMatch only does a partial deep comparison so doesn’t work neither)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 11, 2020, 6:56pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/8 "2020-08-11T18:56:11Z")

</div>

At this point the only thing I can think of is to loop through the arrays, try to find a match, and use \_.isEqual on the match.

Not the best solution, but might work.

---

<div class="post-metadata">

**Author:** ![MariDubois](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@MariDubois](https://community.postman.com/u/MariDubois)\
**Post date:** [August 12, 2020, 8:01am UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/9 "2020-08-12T08:01:00Z")

</div>

@allenheltondev Hello thank you for your suggestion.  
I figured out a way that works for me. I post it here in case someone looking for the same.

```
function customizer(objValue, srcValue, key, object, source) {
     
   if(Object.keys(object).length !== Object.keys(source).length){
     return false;
   }

  if(typeof objValue ==='object' && typeof srcValue === 'object'){
        if(Object.keys(objValue).length !== Object.keys(srcValue).length ){
                    return false;
        }
      }
      if (Array.isArray(objValue) && Array.isArray(srcValue)) {
           if(!(objValue.length === srcValue.length)){
              return false;
            }
        
      }
  }

console.log(lodash.isMatchWith(json1,json2,customizer))
```

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 12, 2020, 11:37am UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/10 "2020-08-12T11:37:08Z")

</div>

Nice work! That should do it 🙂

---

<div class="post-metadata">

**Author:** ![MariDubois](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@MariDubois](https://community.postman.com/u/MariDubois)\
**Post date:** [August 12, 2020, 2:18pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/11 "2020-08-12T14:18:02Z")

</div>

Hello

This works well to identify if two json are the same disregarding order. However I didn’t figure out a way to print the diff 😕

So for me solves half of my problem.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 12, 2020, 3:01pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/12 "2020-08-12T15:01:45Z")

</div>

You could iterate over the keys of one object, see if there is a matching key/value in the other one, and if no match, you could print it out.

Then you would iterate over the other object and do the same thing.

Again, it’s not elegant and is a decent amount of code, but seems like you know your way around javascript.

---

<div class="post-metadata">

**Author:** ![MariDubois](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@MariDubois](https://community.postman.com/u/MariDubois)\
**Post date:** [August 12, 2020, 3:20pm UTC](https://community.postman.com/t/deep-equal-in-any-order/11509/13 "2020-08-12T15:20:49Z")

</div>

Hello

This doesn’t work well for example if you have an item in source that is not present in the other json. I created an issue for this: [https://github.com/lodash/lodash/issues/4900](https://github.com/lodash/lodash/issues/4900)
