Quantcast
Channel: Question and Answer » json
Viewing all articles
Browse latest Browse all 148

How can I loop through a json result by substituting field/column names with a variable

$
0
0

How can I loop through a json result by substituting column names for a variable

End result: I’m building a table from a list (~25 columns). I want to attach custom classes to each cell based on the cell’s value during the loop to parse json data.

I made a REST call for list items in one function, and am looping through the results in another.

  1. I’m using the for loop for(i=0; i<data.d.results.length; i++)
  2. I’m building the table within the loop

$displayTable = $displayTable + $cellClass + $eventClass + data.d.results[i].columnName_1 + $cellClose;

$displayTable = $displayTable + $cellClass + $eventClass + data.d.results[i].columnName_2 + $cellClose;

  1. I want to attach an additional class to all instances based on their values using an if, statement.

  2. Is there a way to loop through the json result by substituting column names for a variable? It’ll make it easier both to write comparison statements with each value on the list and make it easier to display (loop) all columns without writing out 25 column names.

An example will be using a $cellInstance variable variable instead of a column/field name as seen in point 2 above. This would be able to

“$displayTable = $displayTable + $cellClass + $eventClass + data.d.results[i].$cellInstance + $cellClose;`


Viewing all articles
Browse latest Browse all 148

Trending Articles