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

So it is seeing each pair as a single element, which I don't know if that is what you want.   I think writing the values in a string with a += and then splitting it will give you 6 different elements, not 3 separate pairs.


In that case then, you need to use the split(',') just like others suggested and then loop it with indexOf to check the particular value.



var formTypeList = [];


var docSet = new GlideRecordSecure('forms');


docSet.query();


while(docSet.next())


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


var formTypeListarray = formTypeList.split(',');



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


if(formTypeListarray[i].indexOf('YOUR TEXT') > -1) //Returns -1 if the item is not found.


gs.addInfoMessage(formTypeListarray[i]);


}



Or you can use the JavaScript String indexOf() Method instead of JavaScript Array indexOf() Method to check if that particular string is there or not.in formTypeList


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


Thanks everyone so much for the help!   mallen_nspi and wwar1ace, both your   methods work great, thanks!


Hey wwar1ace, I do have a follow up question if you have the time.   I did if(strList.indexOf(formReview.getDisplayValue('form_type'))>-1) from another table that has SF181, SF256, and SF1152 and it returned the correct forms (SF181 and SF256).   However when I do an indexOf to the array (if(listUniqueForms.indexOf(formReview.getDisplayValue('form_type'))>-1)), it only returns SF256.   Obviously I will use the indexOf to the strList, but is there a reason one works over the other?   Does indexOf perform different between comma separated lists and arrays?



This also happens with mallen_nspi's solution as well.



Thanks again to you both!