Description

Calls the provided function for each row of the provided query and removes the row from query if the function returns false.

Returns

Filtered query.

Category

Query functions

History

New in Adobe ColdFusion (2016 release)

See also

QueryEachQuerySortQueryKeyExists

Syntax

QueryFilter(Object query, UDFMethod func)

Parameters

Parameter Description
query (Required) Query to be iterated over.
func (Required) Function to be called with each row of the query. Should return a boolean value.

Example

<cfscript>
               qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
               sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);
               
               function filterQuery(any Obj){
                              return (Obj.ISSPOTLIGHT == "Y" ? true : false);
               }

               WriteDump(QueryFilter(sampleQuery, filterQuery));
</cfscript>

The script filters the query result to display objects, where IsSpotlight is “Y”. The script returns an array of structs. 

Using member function

<cfscript>
       myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
       // Execute the function to call each row and return the values of Location
       status=myResult.filter(function (city){
             return (city.LOCATION=="Newton" || city.LOCATION=="San Francisco" ? true : false);
                    
       });
       // Display the values that meet the filter requirements
       WriteDump(status);
</cfscript>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy