splitting multiple lists, then joining into single array

davilu
Mega Sage

Hi Everyone, I have a question regarding comma separated lists and arrays.   Below is my simple server script that queries a Forms table and returns back all the forms listed for each row:

var docSet = new GlideRecordSecure('forms');

docSet.query();

while(docSet.next()){

        data.formType = docSet.getDisplayValue('form_type');

gs.addInfoMessage(data.formType);

}

The gs info message returns this, which is correct:

find_real_file.png

I want to split all of the comma separated items and return it all in the same array.

sampleArray = [ sf 61, sf306, sf2817, sf 2808, sf256, sf181 ]

I've tried samplelist.split(','), but can't seem to get it to work correctly.   Can someone help with this syntax?

1 ACCEPTED SOLUTION

Brian Dailey1
Kilo Sage

Hi David,



I don't know if this matters to your requirement, but I like to use an object as an intermediate to eliminate duplicates when building lists from multiple sources.




var docSet = new GlideRecordSecure('forms');  


docSet.query();



var uniqueForms = {};


while(docSet.next()){  


        var docSetForms = docSet.getDisplayValue('form_type').split(',');  


        //Using the value also as the key for uniqueForms{} will result in only one instance of any value.


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


                uniqueForms[docSetForms[i]] = docSetForms[i];


        }


}



var listUniqueForms = [];



for(var m in uniqueForms){


      listUniqueForms.push(m);


}


var strList = listUniqueForms.join();



//Either return as an array (listUniqueForms)


//Or as a string (strList).





Thanks,


-Brian


View solution in original post

11 REPLIES 11

Mike Allen
Mega Sage

Do this:


var listList = '';


var sampleArray = [];


while(docSet.next()){  


          listList += docSet.getDisplayValue('form_type') + ',';  


        gs.addInfoMessage(data.formType);  


}  


data.formType = listList;



sampleArray = data.formType.split(',');



Or just do a push.



var sampleArray = [];


while(docSet.next()){  


          sampleArray.push(docSet.getDisplayValue('form_type'));


}  



Or something like that.   Have not worked too much with GlideRecordSecure, but it works in GlideRecord.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello David,



Sample code here.



var arr1 = [];


var docSet = new GlideRecordSecure('incident');  


docSet.addQuery('sys_id','965c9e5347c12200e0ef563dbb9a7156');


docSet.query();  


while(docSet.next()){  


var data.formType = docSet.getDisplayValue('form_type').split(',');


}



for(var i=0;i<data.formType.length;i++)


{


arr1.push(data.formType[i]);


}



for(var s = 0; s<arr1.length; s++)


{


gs.addInfoMessage(arr1[s]); //gets the value


}


Shishir Srivast
Mega Sage

Hi David,



You want to split all of the comma separated items and return it all in the same array, but why you want to split and then get in an array again? I think using push() function it will add the next value with comma, please check if this helps.



var formTypeList = [];


var docSet = new GlideRecordSecure('forms');  


docSet.query();  


while(docSet.next())


        formTypeList.push(docSet.getDisplayValue('form_type'));


gs.addInfoMessage(formTypeList);


Thanks Shishir,



I wanted everything in one array so that I can use indexOf from another array to match up forms.   Is that still doable using the push method?   I wasn't   sure that would work since when I do:



data.formType.push(docSet.getDisplayValue('form_type'));


gs.addInfoMessage(data.formType[2]);



I see this as the info message:



find_real_file.png



Would that work with indexOf?