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

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>


Thank you! The only change I had to make was to use phase 2 rather than phase 1.



Can you explain why I have to go through the extra step of an evaluate tag? It doesn't make sense to me why I can dot-walk to a field in a GlideRecord, but I can't do the same for a plain javascript Object.



Thanks again.


Rachel,



Yeah, I noticed that mistake about the phases. Something I still have to consciously make an effort to check.


Anyway, I'm not sure why but I think for the forEach's var attribute, the correct representation of the type of var gets lost and it might view it as just a string. Not sure.



Using the jelly syntax keeps it as an object (if object attribute is set). Honestly I really didn't know if it would work. But I'm glad it did.


Hi rachel.erickson , it seems that in Helsinki (maybe since Geneva) there is no need to do a extrat step.



Cheers