How to add double quotes to array values

Shalani Rajendr
Tera Contributor

Hi All,

I need to add double quotes to values in an array please help me on how to do that.

Example: var arr=["servicenow,community"];

I have an array like this but i need the array as var arr=["servicenow","community"]; via server script

Please tell me on how to solve this

 

1 ACCEPTED SOLUTION

Vitali1
Tera Expert

It looks like you have a value as a string but need to have multiple values instead.
You could try to split this string into multiple values before putting it in the array.
try this one

var someValue = "servicenow,community";
arr = someValue.split(',');
gs.print(arr);

View solution in original post

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

ideally when you declare an array every element is wrapped with double quotes or single quote

the example for array you shared is not the correct way to declare it

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Yes you are correct but im getting the value like that so asked is there any way?

Hi,

so you can get the array value and then split them

how and from where the array is getting populated?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Vitali1
Tera Expert

It looks like you have a value as a string but need to have multiple values instead.
You could try to split this string into multiple values before putting it in the array.
try this one

var someValue = "servicenow,community";
arr = someValue.split(',');
gs.print(arr);

Upender Kumar
Mega Sage

try below

var ar=[];
var as="servicenow";
var ab="community";
ar.push('"'+as+'"');
ar.push('"'+ab+'"');

gs.print(ar.toString());