在 Mule 4 (DW2.0) 中比较子集数组和主数组
Compare a sub set array with a master array in Mule 4 (DW2.0)
我有一个固定元素数组:['a', 'b', 'c', 'd']
这将用作比较输入数组(可以是主数组的子集)时的基础
我得到一个可能满足以下场景集的各种组合的输入数组:
['a', 'c']
应该 return true
— 可以是主集的子集
['a', 'b', 'd', 'c']
should return true
— 没有顺序限制,可以和master set一样
['a', 'b', 'c', 'd', 'e']
应该 return false
— 不能包含附加元素
['e', 'f']
应该 return false
— 没有找到匹配的元素
最后:
['a']
应该 return true
— 可以是子集并且也可以包含单个元素,但是单个元素应该始终是 'a'
['b','c','d']
应该 return false
— 所有输入数组必须至少包含元素 'a'
所以您需要做的基本上是检查第一个元素是否匹配,然后检查它们是否都存在于 test
数组中。
%dw 2.0
output application/json
import * from dw::core::Arrays
var test= ['a', 'b', 'c', 'd']
var value = ['a']
---
test[0] == value[0] and (value every ((item) -> test contains item ))
%dw 2.0
output application/json
var mainset = ['a', 'b', 'c', 'd']
var subset = ['a', 'c']
---
{
isSubset : isEmpty(subset -- mainset) and contains(subset,'a')
}
我有一个固定元素数组:['a', 'b', 'c', 'd']
这将用作比较输入数组(可以是主数组的子集)时的基础
我得到一个可能满足以下场景集的各种组合的输入数组:
['a', 'c']
应该 return true
— 可以是主集的子集
['a', 'b', 'd', 'c']
should return true
— 没有顺序限制,可以和master set一样
['a', 'b', 'c', 'd', 'e']
应该 return false
— 不能包含附加元素
['e', 'f']
应该 return false
— 没有找到匹配的元素
最后:
['a']
应该 return true
— 可以是子集并且也可以包含单个元素,但是单个元素应该始终是 'a'
['b','c','d']
应该 return false
— 所有输入数组必须至少包含元素 'a'
所以您需要做的基本上是检查第一个元素是否匹配,然后检查它们是否都存在于 test
数组中。
%dw 2.0
output application/json
import * from dw::core::Arrays
var test= ['a', 'b', 'c', 'd']
var value = ['a']
---
test[0] == value[0] and (value every ((item) -> test contains item ))
%dw 2.0
output application/json
var mainset = ['a', 'b', 'c', 'd']
var subset = ['a', 'c']
---
{
isSubset : isEmpty(subset -- mainset) and contains(subset,'a')
}