Parameter
Description
Filters the key-value pairs in a struct.
Returns
Filtered struct.
Category
Syntax
structFilter(struct,callback [, parallel] [, maxThreads])
History
ColdFusion (2021 release): Introduced the following parameters:
- parallel
- maxThreadCount
ColdFusion (2018 release): Introduced named parameters.
ColdFusion 10: Added this function.
Parameters
|
Description |
struct |
Name of the struct object. |
callback |
Inline function executed for each element in the array. Returns true if the key value pair in the struct has to be included in the resultant struct. |
parallel |
(Boolean)- True if you want to enable parallel programming. |
maxThreads |
(Int) The number of threads the function can execute. The number of threads must be between 1-50. If the value exceeds 50, there is an exception. |
Example
<cfscript> myStruct={CF1=4.5,CF2=6,CF3=7,CF4=8,CF5=9,CF6=10,CF7=11,CF8=2016}; WriteOutput("The input, unfiltered struct is:"); WriteDump(myStruct); myFilteredStruct=StructFilter(myStruct,function(key,value){ return value>8; //Return only those values >8 } ); WriteOutput("The filtered struct is:"); WriteDump(myFilteredStruct);//Display the filtered struct </cfscript>
Output
Using member function
<cfscript> myStruct={CF1=4.5,CF2=6,CF3=7,CF4=8,CF5=9,CF6=10,CF7=11,CF8=2016}; CFVersions=myStruct.filter(function(key,value){ return value>8; }); WriteDump(CFVersions); </cfscript>
Using parallelization
<cfscript> mystruct= Structnew("ordered"); for(i=1;i<=10000;i++){ mystruct.insert("key#i#","val#i#") } function callback(key,val){ if(isObject(val)) return false else if(isSimplevalue(val) && key eq 'key889') { return true } else return false } t_start=GetTickCount() writedump(mystruct.filter(callback)) t_end=GetTickCount() writedump("Time taken with no parallel:" & t_end-t_start) t_start=GetTickCount() writedump(mystruct.filter(callback,true,5)); t_end=GetTickCount() writedump("Time taken with 5 threads:" & t_end-t_start) t_start=GetTickCount() writedump(mystruct.filter(callback,true,10)) t_end=GetTickCount() writedump("Time taken with 10 threads:" & t_end-t_start) t_start=GetTickCount() writedump(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=20)) t_end=GetTickCount() writedump("Time taken with 20 threads:" & t_end-t_start) t_start=GetTickCount() writedump(structfilter(struct=mystruct,callback=callback,parallel=true,maxthreadcount=40)) t_end=GetTickCount() writedump("Time taken with 40 threads:" & t_end-t_start) </cfscript>