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
false
Lucee 4.5+ true if the items can be executed in parallel
Values:
truefalse
maxThreads numeric
Default: 20
20
Lucee 4.5+ the maximum number of threads to use when parallel = true
Links more information about structFilter
- Map, Reduce and other Higher Order Functions - A Primer for map, reduce, filter, and Higher Order Functions in general.
- ColdFusion 11: .map() and .reduce() - A look at the new .map() and .reduce() methods that each of array, struct and lists all have
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}
© 2012–present Foundeo, Inc. and the CFDocs contributors.
Licensed under the MIT License.
ColdFusion is a trademark of Adobe Systems Incorporated.
https://cfdocs.org/structfilter