Filtering a CFML array of structures

For some reason I don’t fully understand, I am now working in a ColdFusion shop. I mean, it is not like it’s a SharePoint shop, but still.

Anyway, I am busy building an assessment tool to assess how people do in courses, almost all of which are bespoke, and I have a nice simple pattern for building questions that involves an array of question structures.

So good so far, but when I want to generate a page with just the questions for that page, I need some way to say which questions to grab. Simple enough, right? Just grab every element in the array that says, “Put me on page X!” Of course, I am still learning CFML, so this simple task involves lots of Googling.

Google helpfully points me to an Array functions page on Adobe. It also points to plenty of nifty blog and StackOverflow pages that discuss the functions available on this page and the Structure functions page. There I find nothing that really works.

I mean, StructFindValue returns a structure that contains the array elements in question, but that requires I either walk it to pull those elements back out again or write two different processor loops, one for structures and one for arrays.

So then I found suggestions for ArrayFind and ArrayFindAll. ArrayFindAll at least returns an array of array positions that indicate the elements we need. But I still have to walk it to pull out those elements from the original array. But then I realized that neither of those array functions are listed in the list of array functions Google wanted to point me to.

The problem, it turned out, is I was looking in the wrong place, many times over. Not only the wrong page, but the wrong server entirely. I was on, when I wanted to be on Not only that, but I didn’t want to be on the Array functions page, but the ColdFusion functions a-b page. And, what to my wondering eyes did appear, but a function called ArrayFilter. Now this is a newer function, and the help-no-x site only covers up to CF9, but all my searches never suggested it, none of the blogs or suggestions in StackOverflow did either. There are some nice complicated workaround though.

So …

Q: How do I filter an array of structures based on values in that structure?

Simple. Use ArrayFilter.

The test is to ensure that the property is defined, otherwise the function will go boom.

There, I answered my own question.


Leave a Reply

Your email address will not be published. Required fields are marked *