How to convert this string to an array in ServiceNow server side script

Snehal13
Kilo Sage
 
7 REPLIES 7

davidwarner007
Tera Contributor

To convert a string to an array in ServiceNow server-side script, you can use the split() method. The split() method splits a string into an array of substrings based on a specified separator.

Here's an example code snippet that demonstrates how to convert a string to an array using the split() method in ServiceNow server-side script:

 
var myString = "apple,banana,orange"; var myArray = myString.split(",");

In this example, the string "apple,banana,orange" is split into an array of three elements using the comma (",") separator. The resulting array will look like this:

 
["apple", "banana", "orange"]

You can adjust the separator parameter of the split() method to fit your specific use case...

 

Rahul Kumar17
Tera Guru

Hi Snehal,

 

To convert a comma-separated string to an array in ServiceNow server-side script, you can use the split() method. Here's an example script:

var csvString = "apple,banana,orange";
var myArray = csvString.split(",");

// Now myArray contains ["apple", "banana", "orange"]

 

In this example, the split() method is called on the csvString variable with a comma as the separator argument. This returns an array of strings that are separated by commas in the original string. The resulting array is assigned to the myArray variable.

You can replace the csvString variable with the string you want to convert to an array in your ServiceNow server-side script.

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Hi Snehal,

 

Please accept the solution. If it's working for you.

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar