Search code examples
muleanypoint-studiodataweavemulesoft

filter and range with map are not working


my need is to go over a list but extract only the first two elements out of that list.

i wrote the following code

payload.list.*listItem filter ($.type == "supervisor") map {

// other code

}

this worked perfectly fine. Then I modified it to extract the first two elements only like this

payload.list.*listItem filter ($.type == "supervisor") [0 to 1] map {

// other code

}

and it doesnt work anymore. i am getting no error but the other code doesnt work as if the map operator has nothing to go over.

also i need to make it work for a scenario if there is only one element after the filter is applied. how should i change my code to make this work.


Solution

  • The range selector must be used directly over an Array so you just need to properly do the selection:

    (payload.list.*listItem filter ($.type == "supervisor"))[0 to 1] map {
    
    // other code
    
    }