- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2023 12:52 PM
I have two arrays. The first one is named "appArr" and has these 5 values (all sys_ids):
5f5ff4b21b779d54f22a99ba234bcbf8
9f5ff4b21b779d54f22a99ba234bcbf5
175ff4b21b779d54f22a99ba234bcbf5
535ff4b21b779d54f22a99ba234bcbf6
4f5ff4b21b779d54f22a99ba234bcbec
My second array is named "list" and has these 2 values:
175ff4b21b779d54f22a99ba234bcbf5
9f5ff4b21b779d54f22a99ba234bcbf5
I am trying to remove the values found in my second array from my first array, so when I am finished, these are the values that should be found in "appArr":
5f5ff4b21b779d54f22a99ba234bcbf8
535ff4b21b779d54f22a99ba234bcbf6
4f5ff4b21b779d54f22a99ba234bcbec
I found an old Community thread which shows how you can use the Splice method to remove values from an array (
So, in a Workflow Run Script action, I tried running this code to remove those values from the original array:
//remove rejected records from approved array
for(j = 0; j < appArr.length; j++){
for(k = 0; k < list.length; k++){
if(appArr[j] == list[k]){
appArr.splice(j, 1);
}
}
}
However, when I tried that, it only removed one of the two values, and left me with these values in my original array:
5f5ff4b21b779d54f22a99ba234bcbf8
175ff4b21b779d54f22a99ba234bcbf5
535ff4b21b779d54f22a99ba234bcbf6
4f5ff4b21b779d54f22a99ba234bcbec
As you can see, the value starting with "175" was left in the array when it should have been removed. Is there some issue with my code? Does modifying the array while looping through it cause problems, and prevent multiple splices from working properly?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2023 01:24 PM
Have a look at the diff() method of the out of the box (global) ArrayUtil class: https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_ArrayUtilAPI%23r_AU-di...
This compares the two arrays and returns the difference, so in your case it should work like this:
var filteredArray = new ArrayUtil().diff(apArr, list);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2023 01:24 PM
Have a look at the diff() method of the out of the box (global) ArrayUtil class: https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_ArrayUtilAPI%23r_AU-di...
This compares the two arrays and returns the difference, so in your case it should work like this:
var filteredArray = new ArrayUtil().diff(apArr, list);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2023 08:25 AM
Thank you for the reply! That looks very promising, and seems to work with my initial simple test. I am going to do some more extensive testing, but got pulled away to deal with some urgent matters.
I will post back after I finish my detailed testing, but I am optimistic this is going to work for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2023 12:27 PM
So I ran some more testing, and it appears to be working perfectly!
I was looking at the other functions in there, and lots of good stuff, so I will definitely bookmark that link.
Thanks again!