Create unique array of sys ids
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2023 09:05 PM
Hi everyone,
I have an array of multiple sys ids in that some sys ids are same.
I need to create an unique array my code is working properly in the Background script but not working in script include please help me with this.
If you have any other method that also will be acceptable.
Code:
var abc2 = ['ac5e60f41b835c14e9b220202d4bcb26','43544150dbf41700154ce3334a961904','9afbbac71b3fe054e9b220202d4bcbd7','3aa0f8a3db860454e8aae5644a9619ac','9afbbac71b3fe054e9b220202d4bcbd7','ac5e60f41b835c14e9b220202d4bcb26','9afbbac71b3fe054e9b220202d4bcbd7'];
var arr2 = [1];
var v1;
var ntc;
for (var i = 0; i < abc2.length; i++) {
ntc = abc2[i].toString();
gs.info('value of ntc is:' + ntc);
v1 = arr2.indexOf(ntc);
gs.info('Value of v1 is: ' + v1);
if (v1 == '-1') {
//gs.info(''));
arr2.push(ntc).toString();
gs.info('Was not present in list');
} else {
gs.info('Already present');
}
}
//arr2.toString().shift();
gs.info('array of resp : ' + arr2);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2023 09:44 PM
Hi @shekhar_jain ,
Try below code it will give only unique values of array.
var abc2 = ['ac5e60f41b835c14e9b220202d4bcb26', '43544150dbf41700154ce3334a961904', '9afbbac71b3fe054e9b220202d4bcbd7', '3aa0f8a3db860454e8aae5644a9619ac', '9afbbac71b3fe054e9b220202d4bcbd7', 'ac5e60f41b835c14e9b220202d4bcb26', '9afbbac71b3fe054e9b220202d4bcbd7'];
var au = new ArrayUtil();
var newarr = au.unique(abc2);
gs.info(newarr);
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2023 10:18 PM
Hi Pavan,
Code is working properly in Background script but not working in script include.
I checked the logs it's not giving me an unique array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2023 10:29 PM
Hi @shekhar_jain ,
It will work in script include as well. You can check both array values with and without duplicates in logs.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2023 06:57 AM
Hi @shekhar_jain ,
If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar