I have an application that has a lot of very complex objects that in most cases I only need to access an id value and a name value from. I'd like to reduce the overhead of serializing these large objects to JSON. This is a simple function I created that takes an array of objects and returns an array of structures that contains only two keys, an id and a value. This also needs to be compatible with CF8 servers.
I'd like to get some feedback on ways to make the code more expressive and any ways I can optimize this as well.
<cffunction name="toKeyValuePairArray" returntype="Array" output="false" access="public">
<cfargument name="theArray" type="Array" required="true" hint="an array of objects" />
<cfargument name="idGetterFunctionName" type="string" />
<cfargument name="valueGetterFunctionName" type="string" />
<cfset var local = structNew() />
<cfset var retArray = [] />
<cfset var obj = {} />
<cfloop array="#arguments.theArray#" index="local.theComponent">
<cfset obj = {} />
<cftry>
<cfinvoke component="#local.theComponent#" method="#arguments.idGetterFunctionName#" returnvariable="local.id" />
<cfinvoke component="#local.theComponent#" method="#arguments.valueGetterFunctionName#" returnvariable="local.value" />
<cfset obj.id = local.id />
<cfset obj.value = local.id />
<cfset arrayAppend(retArray,obj) />
<cfcatch></cfcatch>
</cftry>
</cfloop>
<cfreturn retArray />
</cffunction>