CFLib.org – Common Function Library Project

listEach(list, callback[, delimiters])

Last updated October 17, 2013

author

Adam Cameron

Version: 1 | Requires: CF10 | Library: DataManipulationLib

Description:
Iterates over a list, calling a callback for each element. Returns an ARRAY of the results of the callback calls. The callback receives the arguments passed to the listEach(), plus index, element and length.

Return Values:
An array containing the returned values from each callback call

Example:

numbers = "tahi|rua|toru|wha";

function _ucase(){
    return ucase(element);
}

upperCasedNumbers = listEach(numbers, _ucase, "|");
writeOutput(serializeJson(upperCasedNumbers));

Parameters:

Name Description Required
list A list to iterate over Yes
callback A callback to call for each element of the list Yes
delimiters The delimiters the list uses. Defaults to a comma No

Full UDF Source:

/**
 * UDF to loop over a list
 * v1.0 by Adam Cameron
 * 
 * @param list      A list to iterate over (Required)
 * @param callback      A callback to call for each element of the list (Required)
 * @param delimiters      The delimiters the list uses. Defaults to a comma (Optional)
 * @return An array containing the returned values from each callback call 
 * @author Adam Cameron (dac.cfml@gmail.com) 
 * @version 1.0, October 17, 2013 
 */
public array function listEach(required string list, required function callback, string delimiters=","){
    var arr = listToArray(list, delimiters);
    var arrLen = arrayLen(arr);
    var result = [];
    for (var index=1; index <= arrLen; index++){
        arrayAppend(result, callBack(argumentCollection=arguments, index=index, element=arr[index], length=arrLen));
    }
    return result;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson