listFilter

Used to filter an list to items for which the closure function returns true.

listFilter(list, function(listElement, [list]) ) → returns list

Member Function Syntax

someList.filter(function(listElement, [list]) )

listFilter Argument Reference

list
Required

function
Required

Inline closure function executed for each element in the list. Returns true if the list element should be included in the filtered list. Support for passing the original list to the closure function added in CF11 Update 5.

Links more information about listFilter

Examples sample code invoking the listFilter function


Example using a simple numeric comparison

Take an array of struct items and use arrayFilter to return ones of a rating 3 and higher.

fruitArray = [{fruit='apple', rating=4}, {fruit='banana', rating=1}, {fruit='orange', rating=5}, {fruit='mango', rating=2}, {fruit='kiwi', rating=3}];

favoriteFruits = arrayFilter(fruitArray, function(item){
     return item.rating >= 3;
});
writedump(favoriteFruits);

Expected Result: Array with the structs for apple, orange and kiwi.


Example using a member function

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

fruitArray = [{fruit='apple', rating=4}, {fruit='banana', rating=1}, {fruit='orange', rating=5}, {fruit='mango', rating=2}, {fruit='kiwi', rating=3}];

favoriteFruits = fruitArray.filter(function(item){
     return item.rating >= 3;
});
writedump(favoriteFruits);

Expected Result: Array with the structs for apple, orange and kiwi.


Fork me on GitHub