是否存在与包含数组的对象无关的简单匹配,而数组内容的顺序无关紧要? [英] Is there a simple match for objects containing array where the array content order doesn't matter?
问题描述
当我不在乎键的顺序或数组内容的顺序时,我试图比较包含数组的对象.包含很深的内容与我需要的内容非常接近,只是我需要同时使用这两种方式来确保数据中没有缺失的元素.理想情况下,我需要一个不区分顺序的 ==
版本.我下面有什么更好的选择吗?
I was trying to compare objects that contain arrays when I don't care about the order of keys or the order of array contents. Contains deep is close to what I need except that I need to use it both ways around to ensure there are no missing elements in the data. Ideally I want a version of ==
that is order-insensitive. Is there is a better option to what I have below?
* def sample = { user: ['read', 'write', 'append'], public: ['read', 'append'] }
* def test = { public: ['append', 'read'], user: ['write', 'read', 'append'] }
* match sample contains deep test
* match test contains deep sample
推荐答案
仅使用2个包含
.是的,我们还没有设计深层容器,很少需要.
Use 2 contains only
. Yes we haven't designed a deep contains, it is rarely needed.
* def sample = { user: ['read', 'write', 'append'], public: ['read', 'append'] }
* match sample.public contains only ['append', 'read']
* match sample.user contains only ['write', 'read', 'append']
为了完整起见,尽管除非您想重新使用架构片段,否则这可能是过大的,但是您可以这样做:
For completeness, although this may be overkill unless you want to re-use schema pieces, you can do this:
* def user = ['write', 'read', 'append']
* def public = ['append', 'read']
* def sample = { user: ['read', 'write', 'append'], public: ['read', 'append'] }
* match sample == { user: '#(^^user)', public: '#(^^public)' }
这篇关于是否存在与包含数组的对象无关的简单匹配,而数组内容的顺序无关紧要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!