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

An input list object.

function function

UDF or closure object.

delim string

A list delimiter to be used. The default value is comma (,).

includeEmptyFields boolean
Default: false

Boolean. Whether to allow empty fields. Default is false.

Links more information about listEach

Sample code invoking the listEach function

Using a semicolon delimiter.

list = "a;b;c";
listEach(list, function(element,index,list) {
}, ";");

Expected Result: 1:a;2:b;3:c;

List Loop list.listEach()

list = "a;b;c";
list.listEach(function(element,index,list) { 
}, ";");

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

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub