How to check whether multiple values exist within an javascript array ?

I have multiple data in array as shown below:

[
    {
        "companyName": "ABC",
        "companyID": "0123"
    },
    {
        "companyName": "DEF",
        "companyID": "4567"
    },
    {
        "companyName": "GHI",
        "companyID": "8910"
    },
    {
        "companyName": "JHK",
        "companyID": "2345"
    },
    {
        "companyName": "LMN",
        "companyID": "6789"
    }
]

Now if the data in the array are multiple then it will go to the backend route or else if the data in the array is single then it will go to the next policy. Can anyone help me with this how to do this?

0 1 173
1 REPLY 1

I think you want to use a JS policy to get the length of the array, and then set a context variable with that length.

var a = JSON.parse(context.getVariable('VARIABLE_THAT_HOLDS_DATA'));
if (Array.isArray(a)) {
  context.setVariable('array_length', a.length);
}

In a subsequent step, you can evaluate that context variable.

<Step>
   <Name>JS-EvaluateLength</Name> <!-- this runs the above JS -->
</Step>
<Step>
   <Condition>(array_length is null) or (array_length = 0)</Condition>
   <Name>POLICY_TO_EXECUTE_WHEN_ARRAY_DOES_NOT_EXIST_OR_HOLDS_NO_ELEMENTS</Name>
</Step>