Skip inside the for each loop

Hi All,

Consider a scenario where  ,I am looping on an array of 100 values.

I got the value which i was looking at the index of 20 . then i need set 2 variables with predefined values and skip rest of the looping in for each.

Could you please let me know if it is possible ? sample code is really appreciated .

Thanks in advance

Raj

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer
    More detail is needed for when you say "then i need set 2 variables with predefined values". What are the "2 variables" you're setting, what "predefined values", and what is their relation to the value you find at index 20 of the array?

    As far as "skipping the rest of the looping" in a!forEach(), as far as i know this isn't possible as it's not the way it works, but basically if you have the a!forEach expression return an empty set ( "{}" ) when a particular iteration is not a match, then only the matching entries will produce any output (effectively "skipping" any of the iterations that you don't need).
  • Hi Mike,
    Thanks for the reply

    Please find sample code.
    Ex:

    local!a : {"1","2","3","4","5","6","7","8","9","10"}
    local!toset,
    local!sampleJsonObj: "{ 1:null,2:null,3:null, 4:India, 5:australia, 6:null,7:spain,8:null,9:yyyy,0:jjjjj}" /*this array holds json objects with more than 100 */

    For ex: a!forEach(
    items:local!a,
    expression: if ( not (isnull ( index (samplejsonObj, fv!item )) , set the first index value / json object of not null to a local toset or cdt , else skip the next values)
    )

    hope it is clear with pseudo code.

    Thanks
    Rajesh
  • +1
    Certified Lead Developer
    in reply to rajeshr501

    Well I'm not altogether clear what's supposed to be inside your "sampleJsonObj" object since it's currently structured more like a dictionary instead of an array, and not valid JSON text.  But i'll prepare a code example where I assume it's a dictionary (already converted back from JSON) with each entry in its own property named "index1"..."index10".  The same sample can be applied with simple changes in the case that you're using an array here, the rest should work about the same.

    So it sounds like you're asking to return the first(?) index of a non-null entry within the JSON array.  For that first we can simply get the indices of all non-null entries like this:

    with(
      local!a : {"1","2","3","4","5","6","7","8","9","10"},
      local!toset,
      
      local!sampleJsonObj: {
        index1: null,
        index2: null,
        index3: null, 
        index4: "India", 
        index5: "australia",
        index6: null,
        index7: "spain",
        index8: null,
        index9: "yyyy",
        index10: "jjjjj"
      }, /*this array holds json objects with more than 100 */
    
      
      /* get all indices */
      a!forEach(
        items: local!a,
        expression: if( 
          not( isnull( property(local!sampleJsonObj, "index" & fv!item ))),
          tointeger(fv!item),
          {}
        )
      )
    )

     

    Then if you only want the first such index, my suggestion is you simply use the same code and just pull the first value from the resulting array of indices:

    with(
      local!a : {"1","2","3","4","5","6","7","8","9","10"},
      /*local!toset,*/  /* now setting this below */
      
      local!sampleJsonObj: {
        index1: null,
        index2: null,
        index3: null, 
        index4: "India", 
        index5: "australia",
        index6: null,
        index7: "spain",
        index8: null,
        index9: "yyyy",
        index10: "jjjjj"
      }, /*this array holds json objects with more than 100 */
    
      
      /* get all indices */
      local!allIndices: a!forEach(
        items: local!a,
        expression: if( 
          not( isnull( property(local!sampleJsonObj, "index" & fv!item ))),
          tointeger(fv!item),
          {}
        )
      ),
      
      /* capture the first index into local!toSet */
      local!toSet: index(
        local!allIndices,
        1,
        {}
      ),
      
      /* reader-friendly output: */
      "The first non-null index is: " & local!toSet & "." & char(10) &
      "The associated value is: " & property(local!sampleJsonObj, "index" & tostring(local!toSet), "(n/a)")
    )

  • Hi Mike,
    Thanks , with your code .
    It helped me to get an approach
    Thanks again
    Raj
Reply Children
No Data