Description

This function iterates over each row of a query and calls the closure function to work on row of the query. The returned value will be set at the same index in a new query or the provided result query.

Returns

Modified query.

Category

Query functions

History

New in Adobe ColdFusion (2016 release)

See also

QueryReduce

Syntax

Object QueryMap(Object query,  UDFMethod mapFunc [, Object resQuery])

Parameters

Parameter Description
query (Required) The query to be iterated over.
mapFunc (Required) Map function to be called with each row of the query.
resQuery (Optional) Result query whose schema is to be used to populate the results from map function.

Example

<cfscript>
               qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
               sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);

               function mapQuery(any Obj){
                              if(Obj.ISSPOTLIGHT == "Y")
                                             Obj.TITLE = "NEW: " & Obj.TITLE;
                              return Obj;
               }

               newQuery = QueryMap(sampleQuery, mapQuery);
               writedump(newQuery);
</cfscript>

The script prefixes “NEW” on Titles that have Spotlight set to “Y”.

Using member function

<cfscript>
       myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
       myArray=ArrayNew(1);
       
       // Populate myArrray with values "NO"
       for (i=1;i<=myResult.recordcount;i++){
             myArray[i]="NO";
             
       }
       // Add column InSanFrancisco and populate with values of myArray
       QueryAddColumn(myResult,"InSanFrancisco","varchar",myArray);
       // Iterate through the array and replace values of InSanFrancisco where location is San Francisco
       status=myResult.map(function (col){
             if (col.LOCATION=="San Francisco")
                    col.InSanFrancisco = "YES";
                    return col;
             
       });
       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