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 10:06 AM
thank you I'll give it a try, even though I found another solution to my problem.
I was setting the objects into the array in a while loop. and now my code immediately prevents the creation of double objects, instead of writing the objects and then removing them again. (Should have thought of this earlier...)
but the uniqueness of an object. I don't set anything specific.
var objShipto = {};
var getId = shiptoGr.purchase_order.ship_to;
var getDisplay = shiptoGr.purchase_order.ship_to.getDisplayValue();
objShipto.sysid = getId;
objShipto.display = getDisplay;
shipto.push(objShipto);
and this line of code might contain the same values during the while loop. how do I set a unique value to an object? I think I missed that part somewhere...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2016 12:48 PM
I know this is a late response, though it may be helpful to someone else.
Using JavaScript array functions, you can capture all necessary data without using a forEach loop.
In order to add items to an array, use the "push" method. For example:
var rowArr = new Array();
rowArr.push(row1);
rowArr.push(row2);
rowArr.push(row3);
//The resulting array would be [row1, row2, row3]
In order to remove items from an array (queue), use the "shift" method. Decomposing the array created above as an example:
var row1 = rowArr.shift(); //Equivalent to rowArr[0]
var row2 = rowArr.shift(); //Equivalent to rowArr[1]
var row3 = rowArr.shift(); //Equivalent to rowArr[2]
In order to remove items from an array (stack), use the "pop" method. Decomposing the array created above as an example:
var row3 = rowArr.pop(); //Equivalent to rowArr[2]
var row2 = rowArr.pop(); //Equivalent to rowArr[1]
var row1 = rowArr.pop(); //Equivalent to rowArr[0]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-27-2016 12:21 AM
Hi,
Try out this snippet. Its a crude solution but modifies your code the least
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate>
var gr = new GlideRecord('u_uksystemstatus');
var statuses = new Array();
var servicestates = new Array();
var index = 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;
}
var initial = 0;
</g:evaluate>
<j:while test="${initial != i}">
<li>${statuses[initial]} ${servicestates[initial]}</li>
<g:evaluate>
initial = initial +1;
</g:evaluate>
</j:while>
</j:jelly>
Let me know if this was helpful
Thanks,
Prithviraj Chaudhuri