Adding array to text field/label

servicenow14710
Tera Expert

Hello developers, im trying to set an array to a label/text field with comma separated. 

lets suppose arr = [a,b,c];

in Text field it is printing as a,b,c,

At the end it is printing comma as  im spliting an arr in comma and populating this.

  var myStr = myArr.toString();
             var outputField = myStr + "," ;

How can i remove this last comma which is adding. Any help is appreciated. Thanks!

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

Hi @servicenow14710 ,

 

Just do it this way

 

 var outputField = myStr ;

 

Thanks,

Danish

 

View solution in original post

2 REPLIES 2

Danish Bhairag2
Tera Sage
Tera Sage

Hi @servicenow14710 ,

 

Just do it this way

 

 var outputField = myStr ;

 

Thanks,

Danish

 

Krushna R Birla
Kilo Sage

Hi @servicenow14710 

You can utilize the following code snippet to remove the last comma:

 

var myArr = ["a", "b", "c"];
var myStr = myArr.join(","); // Convert array to string with commas
gs.info(myStr);  //Output: a,b,c
 
or simly you can use below line instead of join(","). Remove  +"," ;  from your code
var myStr = myArr.toString();
 

Simply copy and paste this code into your background script and verify the output.

 

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

 

Feel free to connect with me on LinkedIn.

 

Best Regards,

Krushna Birla

Senior Software Engineer

Globant, Pune