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

@sureshloganathan 

I think you want to merge the sys_id from getExistingModel function and the array returned from getModelSysIds function and then get the unique

please try this; highlighted in bold

var company = 'Test';
var name = '230820201029';
var getNewModel;
var newModel = 'Network Gear,UNIX Server';

var modelName = company+' '+name;
var sysIds = new TATAHWModelMatrix();

var availableIds = sysIds.getExistingModel(modelName); // Get existing Model-Model Categories sysid

var newIds = sysIds.getModelSysIds(newModel);   // Get new model - model categories sys id
var getNewModel = [];

getNewModel.push(availableIds.toString()); // push available sys_id

var arrayUtil = new ArrayUtil();

var newIdsArray = newIds.toString().split(',');

var finalArray = arrayUtil.concat(newIdsArray,getNewModel)

gs.log('Existing:'+availableIds);
gs.log('New one:'+newIdsArray);
gs.log('Conclusion:'+finalArray);

gs.print('Interception:'+arrayUtil.unique(finalArray));  // duplicate is not removed

Regards
Ankur

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

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

Thanks Ankur. It was worked when I change the array definition in arrayutil.unique function. Thanks a ton.

Regards,

Suresh.

Regards,
Suresh.