Create unique array of sys ids

shekhar_jain
Tera Contributor

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);

 

5 REPLIES 5

Pavankumar_1
Mega Patron

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);
If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

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.

Hi @shekhar_jain ,

It will work in script include as well. You can check both array values with and without duplicates in logs.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

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.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar