Undefined array elements
This seems to come up every now and again on the house of Fusion cf-talk list. Basically, arrays can contain undefined values which cause all sorts of problems. Here’s a piece of code that demonstrates the problem:
The trouble is that myArray is undefined. Thinking back to the article on the underlying java methods of the array object (see coldfusion objects are java objects), I came up with this, I think elegent, solution to the problem:
So now we can rewrite our original example as:
And there we have it. Usefully, the original array remains untouched due to arrays being the only ColdFusion objects not passed by reference. This means that you can get a copy of the array without undefined elements should you need the original intact.
Of course, it would be better to fix the problem at the source, i.e. stop the undefined elements being ‘created’ in the first place. This may not always be possible though, so a solution like this might come in handy.