How to retrieve element from array in a jelly tag
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2014 08:04 AM
Sorry I understand this might be a very simple question, but despite my research I've hit a wall here. Here is my code below
<g:evaluate> var gr = new GlideRecord('u_uksystemstatus'); var statuses = new Array(); var servicestates = new Array(); var i = 0; gr.query(); while(gr.next()) { statuses[i] = gr.u_servicetitle.toString(); servicestates[i] = gr.u_servicestate.toString(); i = i + 1; } </g:evaluate> <j:forEach var="jvar_word" items="${words}"> <p>${jvar_word}</p> </j:forEach> <j:forEach var="jvar_status" items="${statuses}"> <li>${jvar_status} ${servicestates}</li> </j:forEach> </j:jelly>
My goal is to display each status in the statuses array with the corresponding service state, but I'm not sure how to retrieve a specific element in an array that corresponds to the element that is being retrieved in the <j:forEach> tag. Does anyone have any ideas?
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2014 05:32 AM
Hey,
You don't actually need to put those in an array. You can simply use a while loop.
<g:evaluate>
var gr = new GlideRecord('u_uksystemstatus');
gr.query();
</g:evaluate>
<j:forEach var="jvar_word" items="${words}">
<p>${jvar_word}</p>
</j:forEach>
<ul>
<j:while test="${gr.next()}">
<li>${gr.u_servicetitle} ${gr.u_servicestate}</li>
</j:while>
</ul>
Otherwise, you are looking for the indexVar in the forEach loop.
<j:forEach var="jvar_status" items="${statuses}" indexVar="jvar_i">
<g:evaluate var="jvar_s" expression="statuses[${jvar_i}]" />
<g:evaluate var="jvar_ss" expression="servicestates[${jvar_i}]" />
<li>${jvar_s} ${jvar_ss}</li>
</j:forEach>
You cannot use standard object notation in the Jelly print tags ${} or $[]. You need to evaluate it first.
- James
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2014 07:36 AM
Can you please help me with this:
I have an array of object, and I want now to remove all duplicate objects... do you have any ideas how to do this inside <g:evaluate>?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2014 08:22 AM
There is an OOB method, but it doesn't retain order:
var arr = [1,2,3,4,5,6,3,5,6];
var au = new ArrayUtil();
au.unique(arr);
// [1,2,4,3,5,6]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2014 08:38 AM
doesn't work, the result returns "undefined(blablabla...)" either because it's objects in the array or because the script include is not properly called. I guess it's because the ArrayUtil.unique() cannot handle objects.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2014 09:57 AM
Last one... it's probably best if you open this one up in a new post to save hijacking this thread...
Strange. It should handle objects just fine.
You might be better of restructuring your code to use an object for uniqueness. I assume there is a unique value on your objects? If so, then create an object of those unique values and check that before you add the object to the array. e.g.
var uniqueMap = {};
var uniqueArr = [];
... loop start
if (!(obj.uniqueVal in uniqueMap)) {
uniqueMap[obj.uniqueVal] = true;
uniqueArr.push(obj);
}
... loop end