pass output in array

Rakshanda Kunte
Tera Contributor

Hi All,

How to pass the output of the script in an array?

 

 

Output is:

Group1

Group2

Group3

9 REPLIES 9

davo
Mega Guru

Hi.

You need to define an array variable such as..

var array  = [];

 

then push your output into it, such as

var gr= new GlideRecord('sys_group');
gr.addQuery("active=true");
gr.query();
while (gr.next()) {
    array.push(gr.name);
}
 
that should work for you

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Rakshanda Kunte ,

 

You can try something like below as well if the output you are not getting from GlideRecord

 

// Your script output

var scriptOutput = "Group1\nGroup2\nGroup3";



// Split the output into an array using newline as the delimiter

var outputArray = scriptOutput.split('\n');

// Now 'outputArray' contains the groups as separate elements
console.log(outputArray); // This will display the array in the console for testing pruposes

Thanks,

Danish

 

Anand Kumar P
Giga Patron
Giga Patron

Hi @Rakshanda Kunte ,

Use below script

AnandKumarP_0-1699977897018.png

var groupArray = [];

var group1 = "Group1";
var group2 = "Group2";
var group3 = "Group3";

groupArray.push(group1);
groupArray.push(group2);
groupArray.push(group3);

gs.print("Array Contents: " + groupArray.join(', '));

Mark it as solution proposed and helpful if it serves your purpose.

Thanks,

Anand

Hi @Anand Kumar P ,

I have 521 groups.

Should I create 521 variables?

 

Thank you..