How to split Comma separated values

sk59
Tera Expert

Hi All,

I am returning multiple location values under each country like below which has comma

Country A - av, mexico

Country A - abo,vig,abanaro

I need to get these locations when a specific country is selected and display only those locations. 

As the locations include comma(,) how do I split and display

I am able to get the values like av, mexico,abo,vig,abanaro in the logs

but how do I set it in the locations field?

 

10 REPLIES 10

Omkar Mone
Mega Sage

Hi 

Can you share you code?

this is an on change client script i.e when country changes respective locations should populate

 

var countryIds = g_form.getValue('country');
var siteIds = g_form.getValue('site');

siteIds = siteIds.split(',');

if (siteIds && siteIds.length) {

getSiteIds(countryIds, function (response) {
response = response ? response.split(',') : [];
alert('siteIds '+response);
siteIds = siteIds.reduce(function (acc, siteId) {
if (response.indexOf(siteId) !== -1) {
if (acc.indexOf(siteId) === -1) {
acc.push(siteId);
}
}

return acc;
}, []).join(',');

g_form.setValue('site', siteIds);

});
}

}

 

 

here the funtion getSiteIds returns the values like below from the script include

AM, Acindar,Ternium, San Nicolas de los Arroyos

 

where

AM, Acindar -location 1

Ternium, San Nicolas de los Arroyos - location 2

Varsha21
Giga Guru

Hi 

Add those location values into an array and display array values with comma.

  e.g  

var locations= [];
locations.push('av');
locations.push('mexico');
locations.toString();

gs.print(fruits);  o/p : av,mexico

as like above you can push values whatever u have and display.

 

Thanks 

Omkar Mone
Mega Sage

You can use split function of javascript inside your code if it is in string format.