- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 09:54 PM
Hi,
I have 2 arrays. I need to get the users not present in the other array. I am using the below code I am able to get unique users but there are duplicates in it.
var arrCapUsers = ["a","b","c","d","e"];
var arrGrpUsers = ["a","b","c","d","e","f","g","h","i","j","k","l"];
var arrFinal = [];
for(var i=0; i<arrCapUsers.length; i++){
for(var j=0; j<arrGrpUsers.length; j++){
if(arrCapUsers[i] != arrGrpUsers[j]){
arrFinal.push(arrGrpUsers[i])
}
}
}
gs.info("arrFinal:"+arrFinal);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 10:28 PM
Hi,
then do this ->use diff() method
var arrCapUsers = ["a","b","c","d","e"];
var arrGrpUsers = ["a","b","c","d","e","f","g","h","i","j","k","l"];
var arrFinal = [];
var arrayUtil = new global.ArrayUtil();
arrFinal = arrayUtil.diff(arrGrpUsers,arrCapUsers);
gs.info(arrFinal);
Output:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 10:05 PM
Hi,
what do you mean by Unique?
you can use ArrayUtil class and it has different functions
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 10:20 PM
I want the output to be as ["f","g","h","i","j","k","l"] discard ["a","b","c","d","e"] as it is present in both the arrays.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 10:28 PM
Hi,
then do this ->use diff() method
var arrCapUsers = ["a","b","c","d","e"];
var arrGrpUsers = ["a","b","c","d","e","f","g","h","i","j","k","l"];
var arrFinal = [];
var arrayUtil = new global.ArrayUtil();
arrFinal = arrayUtil.diff(arrGrpUsers,arrCapUsers);
gs.info(arrFinal);
Output:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 10:10 PM
Hi Hari
on page https://community.servicenow.com/community?id=community_article&sys_id=d5d43229db47d0943daa1ea668961... all required methods of the ArrayUtil class are introduced.
To my mind, you can use diff() and unique() without implementing anything.
Kind regards
Maik