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

setKeys

Last update:
May 18, 2026
Returns keys or elements of the set for listing or conversion.

Description

Returns the elements of the set in a form suitable for key-like access or listing.

Returns

Array or list.

Category

ColdFusion Set member methods

Function syntax

set.keys()
setKeys(set)

Parameters

ParameterDescription
setSet (BIF form).

See also

Example

The following example uses this API in cfscript (syntax: set.keys()).

<cfscript>
    s = setNew();
    s.add("k1");
    s.add("k2");

    it = setKeys(s);
    while (it.hasNext()) {
        writeOutput(it.next() & " "); // k1 k2 (order not guaranteed for unordered set)
    }
    
</cfscript>
      

Real-world example

Filter a struct to only keys that appear in an allowlist set (use toArray if keys() mirrors elements).

  <cfscript>
    /**
    * Whitelist struct keys using a Set of allowed key names.
    * Prefer toArray() if keys() is unavailable or setKeys does not return an iterator.
    */
    function filterStructByAllowlist(required struct data, required any allowedKeySet) {
        var out = {};
        // Option A: array from set (portable)
        var keyNames = allowedKeySet.toArray();
        for (var k in keyNames) {
            if (structKeyExists(data, k)) {
                out[k] = data[k];
            }
        }
        return out;
    }

    // Example: API may only return id and name
    payload = {
        "id": 100,
        "name": "Ada",
        "secret": "do-not-leak",
        "internalOnly": true
    };

    allowed = setNew();
    allowed.add("id");
    allowed.add("name");

    safe = filterStructByAllowlist(payload, allowed);
    writeOutput(serializeJSON(safe)); // {"id":100,"name":"Ada"}
</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