Whatever message this page gives is out now! Go check it out!

setFilter

Last update:
Jun 9, 2026
Create a new array containing only the elements from an existing array that match a given condition.

Description

Used to create a new array containing only the elements from an existing array that match a given condition. It works by passing each element to a callback (closure or UDF), and only elements for which the callback returns true are included in the result.

Returns

A new Set.

Category

ColdFusion Set helper utilities

Function syntax

utils.setFilter(set, callback)

Parameters

ParameterDescription
setSource set.
callbackInline function executed for each element in the set. Returns true if the set element has to be included in the resultant set.

See also

Example

The following example uses this API in cfscript (syntax: utils.setFilter(set, callback)).

<cfscript>
    s = setNew();
    s.add(1);
    s.add(2);
    s.add(3);
    s.add(4);
    s.add(5);

    filtered=setFilter(s,function(item){
        return item mod 2 eq 0; // Keep even numbers
    })

    writeDump(filtered) // 2,4
</cfscript>
      

Real-world example

Keep only IDs marked active in a lookup struct.

<cfscript>
    tokens = setNew();
    tokens.add("T-1001");
    tokens.add("1002");
    tokens.add("batch");

    ticketIds = setFilter(tokens, function (item) {
        return isNumeric(item);
    });

    writeDump(ticketIds); // 1002
</cfscript>
      

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page