listEach
Iterates over every element of a List object and can call a UDF function, passed as the second argument.
listEach(String str, UDFMethod function [, String delim, boolean includeEmptyFields]); → returns void
Argument Reference
str string Required
An input list object.
function function Required
UDF or closure object.
delim string Required
A list delimiter to be used. The default value is comma (,).
includeEmptyFields boolean
Default: false
false
Boolean. Whether to allow empty fields. Default is false.
Links more information about listEach
- 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 listEach function
Using a semicolon delimiter.
list = "a;b;c";
listEach(list, function(element,index,list) {
writeOutput("#index#:#element#;");
}, ";"); Expected Result: 1:a;2:b;3:c;
List Loop list.listEach()
list = "a;b;c";
list.listEach(function(element,index,list) {
writeOutput("#index#:#element#;");
}, ";"); Expected Result: 1:a;2:b;3:c;
Example 1
empArray = ["john", "pete", "bob"];
listS = "'john', 'pete', 'bob'";
arrayEach(empArray, xclosure);
listEach(listS, xclosure);
function xclosure(empname, index) {
writeOutput(empName & " at index: " & index);
} Expected Result: john at index: 1pete at index: 2bob at index: 3'john' at index: 1 'pete' at index: 2 'bob' at index: 3
Example 2
cityList = "Pittsburgh, Raleigh, Miami, Las Vegas";
function printCity(String city) {
writeOutput("Current city: " & city);
}
listEach(cityList ,printCity); Expected Result: Current city: PittsburghCurrent city: RaleighCurrent city: MiamiCurrent city: Las Vegas
© 2012–present Foundeo, Inc. and the CFDocs contributors.
Licensed under the MIT License.
ColdFusion is a trademark of Adobe Systems Incorporated.
https://cfdocs.org/listeach