arrayUtil search array elements for partial match?

mb8ker
Tera Expert

Hello,

Does anyone know how (or if it is possible) to search an array for partial matches?  I have 2 workflow if activities based on a list collector variable selection that routes approvals.  The list is locations which are separated by "shop" or "office".

What I am trying to do is search the array and return true if any element contains shop or office in the respective If Activities.  My scripts work if I insert a specific location (ex: 2 & 3 below) but I need to return true is any element meets the criteria (a request might include both shop and office locations)

 if (arrayUtil.contains(arrayList, "shop")) 

 if (arrayUtil.contains(arrayList, "Phoenix (shop)")) 
 if (arrayUtil.contains(arrayList, "Houston (office)")) 

 

1 ACCEPTED SOLUTION

phillipkeigley
Kilo Expert

I would not use ArrayUTIL.  I would iterate over the arrayList in a for-loop and make a new array of matches.  

 

var matchesArray=[]; //not tested
for(var u=0; u < arrayList.length; u++) {
if (arrayList[u].indexOf('shop') > -1  || arrayList[u].indexOf('office') > -1 ){
matchesArray.push(arrayList[u].toString());
}
}

 

View solution in original post

3 REPLIES 3

DrewW
Mega Sage
Mega Sage

You are going to have to write your own function to do this like using something like this.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Or build your own Array Util that inherits from the existing one that adds something that does the searching you want.

 

phillipkeigley
Kilo Expert

I would not use ArrayUTIL.  I would iterate over the arrayList in a for-loop and make a new array of matches.  

 

var matchesArray=[]; //not tested
for(var u=0; u < arrayList.length; u++) {
if (arrayList[u].indexOf('shop') > -1  || arrayList[u].indexOf('office') > -1 ){
matchesArray.push(arrayList[u].toString());
}
}

 

Thanks!  That got me in the right direction.  Here is the script I used in case someone else needs them - one in each If Activity in the workflow;

Mix of both shop and office:

answer = ifScript();
var shop = current.variables.location.getDisplayValue().toString(); //conver the variable list to string

var arrayList = shop.split(",");

function ifScript() {
for(var u=0; u < arrayList.length; u++)
	if (arrayList[u].indexOf('shop only') > -1 || arrayList[u].indexOf('office') > -1){
	           
		return 'yes'; 
  
	} else {

          return 'no';
	}

}


Shop Only

answer = ifScript();

var shop = current.variables.location.getDisplayValue().toString(); //convert the variable list to string

var arrayList = shop.split(",");

function ifScript() {
for(var u=0; u < arrayList.length; u++)
	if (arrayList[u].indexOf('shop') > -1){	           
		  return 'yes';
  } else {

          return 'no';

        }

}



Office Only

answer = ifScript();

var shop = current.variables.location.getDisplayValue().toString(); //convert variable list to string
var arrayList = shop.split(',');

function ifScript(){
	
for (var u=0; u < arrayList.length; u++)
		if (arrayList[u].indexOf('office')>-1){       
		return 'yes';
		
	} else {
		 return 'no';
	}
}