ArrayEvery

Note:

You can also set the maximum thread count in ColdFusion Administrator. Click Server Settings > Settings and specify the number of threads in Default Maximum Thread Count For Parallel Functions.

Description

Determines if all elements of an array satisfy a given condition.

Returns

True if all elements match a condition; false, otherwise.

Syntax

arrayEvery(array array, function(item, [index, [array]]) closure, [boolean parallel], [number maxThreadCount])

Member function

arrayObj.Every(callback)

History

ColdFusion (2021 release): Introduced the following parameters:

  • parallel
  • maxThreadCount

ColdFusion (2018 release) Update 5: Added the function.

Parameters

Parameter

Required/Optional

Description

array

Required

Array in which all elements are to be searched.

callback

Required

Function that encapsulates criteria.

parallel

Optional

True if you want to enable parallel programming.

maxThreadCount

Optional

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

Example 1

<cfscript>
 array1=[1,2,3,4,5]
 isEven=(x)=>return x%2==0
 writeOutput(arrayEvery(array1,isEven)) // Returns False
</cfscript>

Example 2

<cfscript>
 arrayAllEven=[2,4,6,8,10,12,14]
 isEven=(x)=>return x%2==0
 writeOutput(arrayEvery(arrayAllEven,isEven)) // Returns True
</cfscript>

Example 3

<cfscript>
 myarray=[2,4,6,8,10,12,14]
 isGT10=(element,index,array)=>return element>10
 writeoutput(arrayEvery(myarray,isGT10)) // Returns True
 writeoutput(myarray.Every(isGT10)) // Returns False
</cfscript>

Exampe 4 - Using named parameters

<cfscript> 
 myarray=[2,5,8,1,4] 
 callback=function(element,index,array){ 
  return element<10 
 } 
 result=ArrayEvery(array=myarray,callback=callback) 
 writeOutput(result) // Returns True 
</cfscript>

Example 5 - Using arrow function

<cfscript> 
 myarray=[2,5,8,1,4,12,29] 
 callback=(element,index,array)=>{ 
  return element>10 
 } 
 result=ArrayEvery(array=myarray,callback=callback) 
 writeOutput(result) // Returns False 
</cfscript>

Member function

<cfscript> 
 array1=[1,2,3,4,5] 
 isEven=(x)=>return x%2==0 
 writeOutput(array1.Every(isEven)) 
</cfscript>

Using parallelization

<cfscript> 
for(i=1;i<=1001;i++){ 
   
  arr[i] = RepeatString("LoremIpsum",i) 
  if (i == 1000) 
   arr[i] = 'wwiwwxxxx' 
 } 
  
  
function callback(item,index){ 
 
if( findnocase('a',item) or findnocase('e',item) or findnocase('i',item) or findnocase('o',item) or findnocase('u',item)) 
return true; 
return false; 
  
 } 
 writeoutput("<br>arrayEvery with parallel set as false: ") 
 writeoutput(arr.every(callback)) 
 writeoutput("<br>") 
  
  
 
 try{ 
  
 writeoutput("<br>arrayEvery with negative number of  threads: ") 
writeoutput(arrayevery(arr,callback,true,-5)) 
}catch(any e){ 
writeoutput("<br>") 
writeoutput("Type: " & e.type & " Message:" &  e.message) 
writeoutput("<br>") 
} 
  
  
  
writeoutput("<br>ArrayEvery with 10 threads: ")  
writeoutput(arr.every(callback,true,10)) 
writeoutput("<br>")  
try { 
writeoutput("<br>ArrayEvery with more than 50 threads: ")  
writeoutput(arr.every(callback,true,100)) 
writeoutput("<br>")   
} 
 
catch(any e){ 
writeoutput("<br>") 
writeoutput("Type: " & e.type & " Message:" &  e.message) 
writeoutput("<br>") 
 
}  
  
writeoutput("<br>ArrayEvery with parallel set as true and with no threads specified, will take thread count from cf admin: ")  
writeoutput(arr.every(callback,true)) 
writeoutput("<br>")   
</cfscript>

Get help faster and easier

New user?