structFilter

Used to filter the key-value pairs in a structure.

structFilter(struct,function(key, value [,struct]){} [, parallel] [, maxThreads]) → returns struct

Member Function Syntax

someStruct.filter(function(key, value [,struct]){} [, parallel] [, maxThreads])

Argument Reference

struct struct
Required

Name of the structure to filter

callback boolean
Required

Closure or a function reference that will be called for each of the iteration. Returns true if the key-value pair in the structure should be included in the filtered struct. Support for passing the original struct to the closure function added in CF11 Update 5.

Callback parameters:

  • key*string : The key for the current iteration
  • value*any : The value for the current iteration
  • struct*struct : A reference of the original struct

parallel boolean
Default: false

Lucee 4.5+ true if the items can be executed in parallel
Values:
  • true
  • false

maxThreads numeric
Default: 20

Lucee 4.5+ the maximum number of threads to use when parallel = true

Links more information about structFilter

Examples
Sample code invoking the structFilter function

Take a struct of items with their rating and use structFilter to return ones of a rating 3 and higher.

fruitRatings = {apple=4,banana=1,orange=5,mango=2,kiwi=3};

favoriteFruits = structFilter(fruitRatings, function(key, value){
     return value >= 3;
});
writedump(favoriteFruits);

Expected Result: {apple=4,orange=5,kiwi=3}

This is the same example, but using a member function on the struct instead of a standalone function.

fruitRatings = {apple=4,banana=1,orange=5,mango=2,kiwi=3};

favoriteFruits = fruitRatings.filter(function(key, value){
     return value >= 3;
});
writedump(favoriteFruits);

Expected Result: {apple=4,orange=5,kiwi=3}

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub