Description

Used to filter the elements of an array.

Returns

A new array

Category

Syntax

arrayFilter(array,filter)

See also

Other array functions.

History

ColdFusion (2018 release): Introduced named parameters.

ColdFusion 10: Added this function.

Parameters

Parameter

Description

array

Name of the array object.

filter

Inline function executed for each element in the array. Returns true if the array element has to be included in the resultant array. The callback function accepts these parameters:

  • item
  • index (optional)
  • array (optional)

For more information, check the blog by Jon Whish. John has detailed examples of using this function.

Example

<cfscript>
     superheroes=[
           {"name":"Iron Man","member":"Avengers"},
           {"name":"Wonder Woman","member":"Justice League"},
           {"name":"Hulk","member":"Avengers"},
           {"name":"Thor","member":"Avengers"},
           {"name":"Aquaman","member":"Justice League"}
     ];

     avengers=ArrayFilter(superheroes,function(item){
           return item.member=="Avengers";
     });
     writeDump(avengers);
</cfscript>

Output

ArrayFilter

Example with callback - Taken from John's blog.

 

<cfscript>
    a = [1,2,3,4,5,6,7,8,9,10];
    result = ArrayFilter(a,function(el, index) {
    return index % 2;
});
    writeDump(result); // [1,3,5,7,9]
</cfscript>