CFLib.org – Common Function Library Project

arraySetEach(array, from, to, callback)

Last updated October 14, 2013

author

Adam Cameron

Version: 1 | Requires: CF10 | Library: DataManipulationLib

Description:
Variation of arraySet() which uses a callback to set each array element. The callback function will be passed all the arguments the call to arraySetEach() received, plus the current array index being set.

Return Values:
A array with specified elements set

Example:

uuids = arraySetEach([], 1, 5, function(){
    return createUuid();
});
writeDump(uuids);

Parameters:

Name Description Required
array An array to set elements within Yes
from The index from which to set elements Yes
to The index to which set elements Yes
callback Function to call to set elements Yes

Full UDF Source:

/**
 * Variation of arraySet() which uses a callback to set each array element
 * v1.0 by Adam Cameron
 * 
 * @param array      An array to set elements within (Required)
 * @param from      The index from which to set elements (Required)
 * @param to      The index to which set elements (Required)
 * @param callback      Function to call to set elements (Required)
 * @return A array with specified elements set 
 * @author Adam Cameron (dac.cfml@gmail.com) 
 * @version 1.0, October 14, 2013 
 */
array function arraySetEach(required array array, required numeric from, required numeric to, required function callback){
    arrayset(array, from, to, "");
    for (var i=from; i <= to; i++){
        array[i] = callback(index=i, argumentCollection=arguments);
    }
    return array;
}

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