structFilter

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

structFilter(struct,function(key, value, [struct]){return true|false;}); → returns struct

Member Function Syntax

someStruct.filter(function(key, value, [struct]){return true|false;});

structFilter Argument Reference

structure
Required

Name of the structure to filter

function
Required

Inline function/closure executed for each key in the structure. 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.

Links more information about structFilter

Examples sample code invoking the structFilter function


Example using a simple numeric comparison

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}


Example using a member function

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}


Fork me on GitHub