CFLib.org – Common Function Library Project

ArrayFilter(array, filter)

Last updated March 31, 2003
Download UDF

author

Raymond Camden                                    Raymond Camden

Version: 1 | Requires: ColdFusion 5 | Library: DataManipulationLib

Description:
Applies a filter to an array (based on array_filter from PHP). For example, taking an array of numbers, you can write a filter UDF that will remove all numbers less than 10.

Return Values:
Returns an array.

Example:

<cfscript>
function noBerry(str) {
    return not findNoCase("berry",str);
}
</cfscript>

<cfset fruit = listToArray("apple,banana,orange,pear,lemon,lime,cherry,strawberry,blueberry")>
<cfdump var="#arrayFilter(fruit,noBerry)#">

Parameters:

Name Description Required
array Array to modify. Yes
filter The UDF, NOT THE NAME, but the UDF to use as a filter. Yes

Full UDF Source:

<cfscript>
/**
* Applies a filter to an array.
*
* @param array      Array to modify. (Required)
* @param filter      The UDF, NOT THE NAME, but the UDF to use as a filter. (Required)
* @return Returns an array.
* @author Raymond Camden (ray@camdenfamily.com)
* @version 1, March 31, 2003
*/

function arrayFilter(array,filter) {
    var newA = arrayNew(1);
    var i = 1;
    
    for(;i lte arrayLen(array); i=i+1) {
        if(filter(array[i])) arrayAppend(newA,array[i]);
    }
    
    return newA;
}
</cfscript>

Search CFLib.org


Latest Additions

Jose Diaz-Salcedo Jose Diaz-Salcedo added
cfRssFeed
3 day(s) ago

Raymond Compton Raymond Compton added
structBlend
23 day(s) ago

Duncan Duncan added
IsZIPUK
23 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
29 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson