How to convert String to Array

shaik_irfan
Tera Guru

Hi,

 

Currnetly i am getting an output as output = abcd, efgh, ijkl

which i want to convert as ["abcd","efgh", "ijkl"]

1 ACCEPTED SOLUTION

vkachineni
Kilo Sage
Kilo Sage

var inputStr = 'abcd,efgh,ijkl';
var outputStr = inputStr.split(',');

var strArr = [];

for(var i = 0; i < outputStr.length; i++) {
strArr.push('"' + outputStr[i] + '"');
}

strArr.join();
gs.info(inputStr);
gs.info(strArr);

*** Script: abcd,efgh,ijkl
*** Script: "abcd","efgh","ijkl"

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

12 REPLIES 12

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You could do something like:

var yourArray = [];

yourArray.push('abcd');
yourArray.push('efgh');
yourArray.push('ijkl');

yourArray.join();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

Hi there,

If your current output is exactly abcd, efgh, ijkl.

var oldOutput = 'abcd,efgh,ijkl';
splitOutput = oldOutput.split(',');

var yourArray = [];

for(var i = 0; i < splitOutput.length; i++) {
yourArray.push(splitOutput[i]); 
}

yourArray.join();

gs.info(yourArray);

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

AbhishekGardade
Giga Sage

Hello Irfa,

Check out this:

var userDetails = [];
var grUser = new GlideRecord('sys_user');
grUser.addActiveQuery();
grUser.setLimit(10);
grUser.query();
while(grUser.next()){

userDetails.push(grUser.first_name);

}

gs.print("Array element:"+userDetails.toString());

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Hello Irfan,

What mark posted is also correct. But my code will be generic. It will store as many strings as you want.

Thanks!
Abhishek Gardade

 

Thank you,
Abhishek Gardade