First off, you don't really need to translate all of that. CF8 doesn't support component/function in cfscript but otherwise you are good to use it as is:
<cfcomponent>
<cffunction name="preserveSession" access="remote" returntype="void" output="no">
<cfargument name="awardData" type="string" />
<cfscript>
var s = {};
var name = '';
var i = 0;
if(!isJSON(arguments.awardData)) return false;
arguments.awardData = deserializeJSON(arguments.awardData);
for(i=1; i<=arrayLen(arguments.awardData); i++) {
name = arguments.awardData[i].name;
if(!structKeyExists(s, name)) {
s[name] = arguments.awardData[i].value;
} else {
if(!isArray(s[name])) {
s[name] = [s[name]];
}
arrayAppend(s[name], arguments.awardData[i].value);
}
}
session.awardFormData = s;
return true;
</cfscript>
</cffunction>
</cfcomponent>
Try simply rewrapping the guts like this rather than rewriting and see if you get farther.
To explain the notation generally:
The s[name]
addresses the structure of s
by key of the value stored in name
; this is sometimes called array notation.
It equivalent to dot notation of s.theValueStoredInName
where the name is known.
To address a structure by a variable name dynamically it is easiest to do this via array notation.
Same thing for the nested set:
The line s[name] = [s[name]]
is setting a key with value of what is stored in name
to the value of what is stored in the named key of s[name]
. By wrapping that set in []
, it is as a type of array.
This will help clarify what is happening here specifically:
If something with that name-key has not already been assigned as a struct, it gets stored as a struct (simple name-key value). If it has, then what ever is in the name-key value gets converted to an array with that name-key. Then if it is a successive pass of a 2nd item (which it will be, the if! is just checking if this array conversion has already occurred at least once), then the 2nd item is appended to that array which has the key of the same name.
Personally, I would just always directly set to an array for simplicity. Then you always know how to address the values in storage.