0

I have a list which contains below values. The list Gets data from restAPI. I know how to compare record but list has many values so i dont know how to search in array or in list. for eg list has below values. I need to check whether particular record exist in list or not.

var items= [
            {
                "organizationCode": "FP1",
                "organizationName": "FTE Process Org"
            },
            {
                "organizationCode": "T11",
                "organizationName": "FTE Discrete Org"
            }, 
            {
                "organizationCode": "PD2",
                "organizationName": "Product development Org"
            },
            {
                "organizationCode": "PD1",
                "organizationName": "Product1 development Org"
            },
            {
                "organizationCode": "MD1",
                "organizationName": "Main development Org"
            }
        ]

        I have to search value based on organizationName.
        I tried 

items.organizationName?contains(<input>)

But not working

I could not get any material so seeking help.

Shruti sharma
  • 199
  • 6
  • 21
  • 67

1 Answers1

1

The problem is that items.organizationName is an error already, since items is purely a list, so it has no named sub-variables directly. (You may saw that pattern when items is coming from XML, where you can get the list of sub-elements like that. But same doesn't work with plain lists.) So, what you need is this expression:

items?map(it -> it.organizationName)?seq_contains(input)

Slightly related, but sometimes you want to retrieve the list of matching items (so you also will have the organizationCode), in which case you should write items?filter(it -> it.organizationName == input).

Note that ?map and ?filter was added in FreeMarker 2.3.29.

See also:

ddekany
  • 29,656
  • 4
  • 57
  • 64