Jaya Krishna N K
Jaya Krishna N K

Reputation: 35

Compare a sub set array with a master array in Mule 4 (DW2.0)

I have a fixed elements array as : ['a', 'b', 'c', 'd'] This will be used as a base while comparing the input arrays (that can be subset of the master array)

I get an input array of various combinations that may satisfy below set of scenarios:

['a', 'c'] should return true — can be sub set of master set

['a', 'b', 'd', 'c'] should return true — no order restrictions and can be same as master set

['a', 'b', 'c', 'd', 'e'] should return false — can’t contain additional element

['e', 'f'] should return false — no matching elements found

and finally:

['a'] should return true — can be sub set and can contain single element too, however that single element should be always 'a'

['b','c','d'] should return false — all input arrays must contain at least the element 'a'

Upvotes: 3

Views: 634

Answers (2)

PK Reddy
PK Reddy

Reputation: 21

%dw 2.0
output application/json
var mainset = ['a', 'b', 'c', 'd']
var subset =  ['a', 'c']
---
{
    isSubset : isEmpty(subset -- mainset) and contains(subset,'a')
}

Upvotes: 2

machaval
machaval

Reputation: 5059

So what you need to do is basically check that the first element matches and then that they are all present in the test array.

%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 ))

Upvotes: 4

Related Questions