How to write for loop in ui macro

Not applicable

I have a requirement where I am getting some array values from script include to ui macro for which I need to write a for loop and set the values to MRVS variable

here is the sample code:

var answer = ['test','test1','test2'];

var obj={};

var arr=[];
var split = answer.split(',');

for(var i=0; i<split.length; i++){

obj = {

'name' : split[i].toString()
};

arr.push(obj);

}

g_form.setValue('mrvs field name',JSON.stringify(arr));
Can someone help me on this 
3 REPLIES 3

Saiganeshraja
Mega Sage
<j:var var="answer" value="['test','test1','test2']"/>

<j:var var="arr" value="[]" />
<j:forEach items="${answer}" var="item">
  <j:var var="obj" value="{ 'name' : '${item}' }"/>
  <j:expr value="arr.push(obj)"/>
</j:forEach>

<j:expr value="g_form.setValue('mrvs_field_name', JSON.stringify(arr))"/>

 

 

 

 

  1. <j:var>: This tag is used to declare and initialise variables in Jelly.
  2. <j:forEach>: This tag is used to iterate over the array elements.
  3. <j:expr>: This tag is used to execute JavaScript expressions within the Jelly script.

 

Rajesh Chopade1
Mega Sage

Hi @Community Alums 

You can try bellow script once:

<g:script>
    // array from the Script Include
    var answer = ['test', 'test1', 'test2'];

    // empty array to store the formatted objects
    var arr = [];

    //push objects to the array
    for (var i = 0; i < answer.length; i++) {
        var obj = {
            'name': answer[i].toString() // Set the MRVS field name to the array value
        };
        arr.push(obj); // Push object to array
    }

    // set value for the MRVS field
    g_form.setValue('mrvs_field_name', JSON.stringify(arr));
</g:script>

i hope my answer helps you to resolve your issue, if yes please mark my answer helpful & correct.

thank you

rajesh

Ravi Gaurav
Giga Sage

Hi,

 

I have tried the script below and it seems to work for me:-

CS:-

function onLoad() {
 
var answer = ['test','test1','test2'];

var arr = [];

for(var i = 0; i < answer.length; i++) {
    var obj = { 'name' : answer[i].toString() };
    arr.push(obj);
}
alert(JSON.stringify(arr));
// Set the MRVS field with the JSON stringified array of objects
g_form.setValue('mrvs_field_name', JSON.stringify(arr));
}
RaviGaurav_0-1724334416989.png