The CreatorCon Call for Content is officially open! Get started here.

How to make array from string

d-aizawa
Kilo Sage

Hi.
In my project, Incident Description(String Type) is described as below.

Table: incident
Summary: testData(2022/08/10 13:00:00)
FirstDate: 2022-08-02 13:00:00
LastDate: 2022-08-10 13:00:00
ID: XX1 TEST_20220810_1234_33
Desc: テスト起票
Group: Help Desk

 

The left is the item and the right is the value, but is it possible to make only the right an array?
If possible, please tell me how to do that.
Thank you!

1 ACCEPTED SOLUTION

Saurabh Gupta
Kilo Patron

Please use below script

 

var t="Table: incident\nSummary: testData(2022/08/10 13:00:00)\nFirstDate: 2022-08-02 13:00:00\nLastDate: 2022-08-10 13:00:00\nID: XX1 TEST_20220810_1234_33\nDesc: テスト起票\nGroup: Help Desk";

//above variable is just to get the value from description field here I used a test string

var tt=t.split("\n");
var fArr=[];
for(var i=0;i<tt.length;i++)
{
    fArr.push(tt[i].split(": ")[1])
}

gs.info(fArr)

Thanks and Regards,

Saurabh Gupta

View solution in original post

4 REPLIES 4

Saurabh Gupta
Kilo Patron

Please use below script

 

var t="Table: incident\nSummary: testData(2022/08/10 13:00:00)\nFirstDate: 2022-08-02 13:00:00\nLastDate: 2022-08-10 13:00:00\nID: XX1 TEST_20220810_1234_33\nDesc: テスト起票\nGroup: Help Desk";

//above variable is just to get the value from description field here I used a test string

var tt=t.split("\n");
var fArr=[];
for(var i=0;i<tt.length;i++)
{
    fArr.push(tt[i].split(": ")[1])
}

gs.info(fArr)

Thanks and Regards,

Saurabh Gupta

Hi @d-aizawa 

 

 


Thanks and Regards,

Saurabh Gupta

Community Alums
Not applicable

Hi @d-aizawa ,

If all you're trying to do is convert a comma separated list of strings to an array, the Javascript .split() method will do that for you without doing anything further.

var strList = 'abcd, efgh, ijkl';
var strArray = strList.split(',');
console.log('list: '+ strList + ' array: ' + strArray);
for (i = 0; i < strArray.length; i++) {
    console.log("strArray["+i+"]: " + strArray[i]);
}

This will almost get it done, but since you have spaces embedded in your list, the actual output may not be exactly what you expect. To wit:

node splitExample.js
list: abcd, efgh, ijkl array: abcd, efgh, ijkl
strArray[0]: abcd
strArray[1]: efgh
strArray[2]: ijkl

Now you could trim each string and push it to an array as seen in other examples here, or you could use the .replace() method in conjunction with .split() to remove the spaces in the string prior to parsing like this:

var strList = 'abcd, efgh, ijkl';
var strArray = strList.replace(/ /g,'').split(',');
console.log('list: '+ strList + ' array: ' + strArray);
for (i = 0; i < strArray.length; i++) {
    console.log("strArray["+i+"]: " + strArray[i]);
}

And that gives us this output:  NOte that the original string is unchanged, but the elements of the array that results are trimmed.

list: abcd, efgh, ijkl array: abcd,efgh,ijkl
strArray[0]: abcd
strArray[1]: efgh
strArray[2]: ijkl

For your reference, I recommend:

https://www.w3schools.com/jsref/jsref_split.asp

https://www.w3schools.com/jsref/jsref_replace.asp

 

Shekhar Navhak1
Kilo Sage

Below script will solve your issue:

 

var disc=current.discription;
var strDisc=disc.split("</br>");
var arr=[];
var val=[];
for(var i=0;i<strDisc.length;i++)
{
     val=strDisc[i].split(": ");
    arr.push(val[1])
}

gs.info(arr)
Please mark the answer correct/helpful based on Impact.
Regards, Shekhar