3

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'

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

4

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 ))
machaval
  • 4,969
  • 14
  • 20
  • This answer might be correct, but it might not. It depends on whether the 'a' must always be the _first_ element in the subset. The question does not state that. – kimbert Apr 14 '20 at 17:56
  • Literal speaking you are right, but imagine that the real example is not that 'a' is required but rather the first element is the one that is mandatory. – machaval Apr 14 '20 at 18:35
2
%dw 2.0
output application/json
var mainset = ['a', 'b', 'c', 'd']
var subset =  ['a', 'c']
---
{
    isSubset : isEmpty(subset -- mainset) and contains(subset,'a')
}
PK Reddy
  • 21
  • 2