0

I am working a serviceNow business rule and want to compare two strings and capture the substrings that are missing from the string for example...

var str1 = "subStr1,subStr2,subStr3,subStr4"
var str2 = "subStr1,subStr3"

magicFunction(str1,str2);

and the magic function would return "subStr2,subStr4"

I'd probably have better luck turning the strings into arrays and comparing them that way which if there is some method that would be recommended I can do that, but I have to push a , separated string back to the form field for it to work right, something with how sys_id's behave seems to demand it.

Basically I have a field on a form that holds a list of sys_ids, I need if one of those sys_ids is removed from the list I can capture the sys_id and make some change on the record belonging to it

Mihai
  • 26,325
  • 7
  • 66
  • 81
mpaquette
  • 65
  • 6

3 Answers3

2

If you're not against using libraries, underscore has an easy way to do this with arrays. See http://underscorejs.org/#difference

function magicFunction(str1, str2) {
   return  _.difference(str1.split(","),str2.split(",")).join(",");
}
BoxOfSnoo
  • 21
  • 3
2

The ArrayUtil Script Include in ServiceNow has a "diff" function, once you use split(",") on your Strings to create two Arrays.

e.g.,

var myDiffArray = new ArrayUtil().diff(myArray1, myArray2);
CapaJC
  • 21
  • 1
1

Assuming you're list has commas separating them, you can use split(",") and join(",") to turn them in to arrays/back into comma delimited lists, and then you can find the differences pretty easily using this method of finding array differences.

Community
  • 1
  • 1
mduleone
  • 73
  • 10