ArrayUtil - Unique function

ersureshbe
Giga Sage
Giga Sage

Hi,

I'm trying to remove duplicates using the arrayUtil function using the below code. But it's not helped to resolve. Can you help me to understand where the issue is? The expected result is to remove the duplicate in x instead it's printing what present in x.

 

var x= [];
x = "82d773951bbe5890454e36d5464bcbaa,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,b1ff7854c3031000b959fd251eba8ff5";
var arrayUtil = new ArrayUtil();
var a1 = new Array(x);
gs.print(arrayUtil.unique(a1));

Regards,

Suresh

Regards,
Suresh.
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Suresh,

few observations

1) you have not declared the array properly

please try this

var x = "82d773951bbe5890454e36d5464bcbaa,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,b1ff7854c3031000b959fd251eba8ff5";

var arr = [];

arr = x.toString().split(',');

gs.info(arr.length);

var arrayUtil = new ArrayUtil();
gs.info(arrayUtil.unique(arr));

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

12 REPLIES 12

Mark Roethof
Tera Patron
Tera Patron

Hi there,

In your example, x is just one large string. That won't work.

This works fine for me:

var x = ["82d773951bbe5890454e36d5464bcbaa","c4d4fe4937101000deeabfc8bcbe5d91","8ffdf454c3031000b959fd251eba8f1c","c4d4fe4937101000deeabfc8bcbe5d91","8ffdf454c3031000b959fd251eba8f1c","b1ff7854c3031000b959fd251eba8ff5"];
var uniqueArray = new ArrayUtil().unique(x);
gs.info(uniqueArray);

Result:

*** Script: 82d773951bbe5890454e36d5464bcbaa,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,b1ff7854c3031000b959fd251eba8ff5

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

 

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 Suresh,

It not working because ,each array elements should be available in '' single/double quotation marks.

Thanks,
Abhishek Gardade

Thank you,
Abhishek Gardade

Hitoshi Ozawa
Giga Sage
Giga Sage

If in your actual script, value of x is from some other operation, you need to convert the String to an array. That is, do .split(",") to convert the string to an array.

var x= [];
x = "82d773951bbe5890454e36d5464bcbaa,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,c4d4fe4937101000deeabfc8bcbe5d91,8ffdf454c3031000b959fd251eba8f1c,b1ff7854c3031000b959fd251eba8ff5".split(",");
var arrayUtil = new ArrayUtil();
var a1 = new Array(x);
gs.print(arrayUtil.unique(a1));

Mark Roethof
Tera Patron
Tera Patron

Any luck on this? Or still help needed?

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

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn