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

When doing GlideRecord queries, ALWAYS be sure to use getValue(). GlideRecord uses a pointer so your array could end up with the same (last) value all the way through.

// Bad
userDetails.push(grUser.first_name);

// Good
userDetails.push(grUser.getValue('first_name'));

Noted your point!!! Thanks for the pointing it out.
Thank you,
Abhishek Gardade

Hello Irfan,

Chcek ou the script after changes suggested:

 

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

userDetails.push(grUser.getValue('first_name'));

}

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

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

Thank you,
Abhishek Gardade

Any update on this?


If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Thank you, Abhishek

Thank you,
Abhishek Gardade

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