CFLib.org – Common Function Library Project

ArrayFilter(array, filter)

Last updated March 31, 2003

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

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<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:

view plain print about
<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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
10 day(s) ago

Will Belden Will Belden added
longTime
16 day(s) ago

James Sleeman James Sleeman added
quickSort
26 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
29 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson