cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Simple For Loop for Infotable Iteration

Highlighted
Amethyst

Simple For Loop for Infotable Iteration

This is using the simplest structure to do a look through an infotable.  It's simple but it avoids having to use row indexes and cleans up the code for readability as well.

 

//Assume incoming Infotable parameter names "thingList"

for each (row in thingList.rows) {

     // Now each row is already assigned to the row variable in the loop

     var thingName = row.name;

}

 

You can also nest these loops (just use a different variable from "row").  Also important to note to not add or remove row entries of the Infotable inside the loop.  In this case you may end up skipping or repeating rows in the loop since the indexes will be changed.

4 REPLIES 4

Re: Simple For Loop for Infotable Iteration

Thanks Tudor,

I think it is useful to know that the same loop is available under the Snippets section in the Composer side, under the "Infotable for loop" name.

BR,

Vladimir

Re: Simple For Loop for Infotable Iteration

Thanks for the feedback Vladimir Rosu.

Adam wrote this code and it differs from the snippet in that it does not use a row index (for  x=0 ... x < tableLength ...).  It's just a little improvement on the snippet to make the code more readable.  I agree -- functionally it is the same

Re: Simple For Loop for Infotable Iteration

to add:

From snippets there is also

// infotable datashape iteration

var dataShapeFields = yourInfotableHere.dataShape.fields;

for (var fieldName in dataShapeFields) {

  //logger.warn('field name is ' + dataShapeFields[fieldName].name);

  //logger.warn('field basetype is ' + dataShapeFields[fieldName].baseType);

}

and there is

Looping through a JSON nested structure

Re: Simple For Loop for Infotable Iteration