Search code examples
muledataweavemulesoftmule4

Find certain strings from an array


I am trying to execute a find function on a payload.

Suppose I have :

["john","abe","john","doe","peter"]

The expected output is

[
"john",
"john",
"peter"
]

I tried to do

["john","abe","john","doe","peter"] find "john" or "peter"

but it doesn't work.

I don't know how to do it.


Solution

  • You can use the filter() function to find items that comply with a condition. As the condition you can have another array of the elements that you are interested and test if the current element is contained in the interested array. Since there is no clear expectation for the output, I choose to return all matching elements. You may want to return something different.

    %dw 2.0
    output application/json
    var choices=["john","peter"]
    ---
    ["john","abe","john","doe","peter"] filter ( choices contains $ )
    

    Output:

    [
      "john",
      "john",
      "peter"
    ]