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
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)") )