Get only unique values

Ratan B
Tera Contributor

Dear community,

Please help me to achieve this. what I want to achieve is to get only unique values from the below 2 variables.

 

var a = '88034e4497021110b549bfb6f053af6d,1364b04297865110b549bfb6f053af5e' ;

var b = '04034e4497021110b549bfb6f053af73,88034e4497021110b549bfb6f053af6d';

I need to get only the unique values as below

var result = '88034e4497021110b549bfb6f053af6d,1364b04297865110b549bfb6f053af5e,04034e4497021110b549bfb6f053af73';

 

But am getting as below, which is not correct.

var result = '88034e4497021110b549bfb6f053af6d,1364b04297865110b549bfb6f053af5e,04034e4497021110b549bfb6f053af73,88034e4497021110b549bfb6f053af6d';

 

Thanks in advance..

1 ACCEPTED SOLUTION

Valmik Patil1
Kilo Sage

Hello,

You need to split your result into array and then use unique method of array util

Please refer below background script

var result = '88034e4497021110b549bfb6f053af6d,1364b04297865110b549bfb6f053af5e,04034e4497021110b549bfb6f053af73,88034e4497021110b549bfb6f053af6d';
var arr = result.split(','); // split your string into array first
var arrayUtil = new ArrayUtil();  
var arr1= arrayUtil.unique(arr);
gs.log(arr1); // unique array

Please let me know if you need any other help

Thanks,

Valmik

View solution in original post

5 REPLIES 5

Harshad Wagh
Tera Guru

just try below

var uniqueResult= new ArrayUtil().unique(result);

Valmik Patil1
Kilo Sage

Hello,

You need to split your result into array and then use unique method of array util

Please refer below background script

var result = '88034e4497021110b549bfb6f053af6d,1364b04297865110b549bfb6f053af5e,04034e4497021110b549bfb6f053af73,88034e4497021110b549bfb6f053af6d';
var arr = result.split(','); // split your string into array first
var arrayUtil = new ArrayUtil();  
var arr1= arrayUtil.unique(arr);
gs.log(arr1); // unique array

Please let me know if you need any other help

Thanks,

Valmik

Hi Valmik,

Thanks for your valuable reply 🙂

@Valmik Patil @Valmik Patil 
Could you please also help me on this 

https://community.servicenow.com/community?id=community_question&sys_id=ba82914adbca1150dd886c8e13961905