How to Access an Array of Objects (Jelly)

rachel_erickson
Kilo Contributor

I'm attempting to access data which I've stored in an array of objects. If I attempt to access the data from within the evaluate tag, I am able to access it. However, when attempting to iterate through the array and retrieve the data from each object, it doesn't work. I have validated that each item in the array is indeed an object, but when I attempt to access a value within that object, no value is returned. Any ideas?

Please note that the example below is not complete yet. Both the number and the question will eventually be required.

UI Page Code:

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<!-- Retrieve questions from table and store in array of objects -->

<g2:evaluate var="jvar_questions" object="true">

  var questions = new Array();

  var grq = new GlideRecord("u_exam_questions");

  grq.query();

  var index = 0;

  while(grq.next()){

            questions[index] = new Object();

            questions[index] = {number: grq.getValue('u_number'), question: grq.getValue('u_question')};

            index = index + 1;

  }

  questions;

</g2:evaluate>

  <p>There are $[jvar_questions.length] questions.</p>

  <ol><!-- Iterate through the question array and display the question text -->

  <j2:forEach items="$[jvar_questions]" var="jvar_question">

            <li>$[jvar_question.question]

            </li>

  </j2:forEach>

  </ol>

</j:jelly>

Output:

There are 2 questions.

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

Hi Rachel,



See if adding another evaluate tag within your forEach to pull out the objects. Something like this:



<ol>


<j2:forEach items="$[jvar_questions]" var="jvar_question">


        <g2:evaluate jelly="true">


          var number = jelly.jvar_question.number;


          var question = jelly.jvar_question.question;


        </g2:evaluate>


            <li>$[question]</li>


  </j2:forEach>


</ol>


View solution in original post

6 REPLIES 6

Thanks, I had the same issue and your solution works for me as well.



Note: In Helsinki (maybe since Geneva) it is possible to access the object properties without doing a g:evaluate. The initial code would work:



<j2:forEach items="$[jvar_questions]" var="jvar_question">


            <li>$[jvar_question.question]</li>


</j2:forEach>


aayush2
Kilo Explorer

Hi All,



Am facing a similar issue -



      <g:evaluate var="jvar_tags"   object="true">


              var tags = [];


              var tagIndex = 0;


              var tagGr = new GlideRecord('x_caci_oracle_clou_cloud_management_tag');


              tagGr.addQuery('category', 'standard');


              tagGr.query();


              while (tagGr.next()) {


                      gs.info("sys_id :"+tagGr.getValue("sys_id"));


                      gs.info("name :" +tagGr.getValue("name"));


                      gs.info("table :" +tagGr.getValue("table"));


                      var idVal = tagGr.getValue("sys_id");


                      var nameVal = tagGr.getValue("name");


                      var tableNameVal = tagGr.getValue("table");


                      tags.push({'id': idVal, 'name': nameVal, 'table': tableNameVal, 'index': tagIndex++});


              }


              var tagsStr = new global.JSON().encode(tags);


              tags;              


      </g:evaluate>






<j:forEach items="${jvar_tags}" var="jvar_tag" indexVar="jvar_index">


                              <g:evaluate var="jvar_tag_id" expression="jelly.jvar_tag.id" jelly="true"/>


                              <g:evaluate var="jvar_tag_name" expression="jelly.jvar_tag.name" jelly="true"/>


                              <g:evaluate var="jvar_tag_table" expression="jelly.jvar_tag.table" jelly="true"/>                    


                             


                      </j:forEach>



//In the above code - ${jvar_tag_name} is coming as null .


// and ${jvar_tag} too is coming in as null