coldfusion根据阵列模数创建四个结构的繁琐方法

时间:2018-06-16 19:14:32

标签: coldfusion structure modulus

我有一个数组“varray”,需要分成四个结构。每个四个元素中的第一个应该在结构1中,第二个在结构2中,等等。我有一些工作代码来做这个,但我觉得应该有一个不那么麻烦的方式。这是代码:

<cfset xord  = StructNew()>
<cfset xsort = StructNew()>
<cfset xsel  = StructNew()>
<cfset xmer  = StructNew()>

<cfloop from = '1' to = "#ArrayLen(varray)#" index = 'i'>
  <cfset fieldname = farray[i]> <!---farray previously defined --->
  <cfset val = varray[i]> <!---varray previously defined --->
 <cfset j = i%4>

 <cfif j EQ 1>
   <cfset xord[fieldname] = val>
 <cfselseif j EQ 2>
   <cfset xsort[fieldname]= val>
 <cfelseif j EQ 3>
   <cfset xsel[fieldname] = val>
 <cfelseif j EQ 0>
   <cfset xmer[fieldname] = val>
 </cfif>

</cfloop>  

有人能建议更好的方法吗?

4 个答案:

答案 0 :(得分:2)

cfscript怎么样?

<cfscript>
function groupByOp(values, fieldnames) {
    var ops = ['mer', 'ord', 'sort', 'sel'];
    var byOp = {};
    arrayEach(values, function (val, i) {
        byOp["x#ops[i % 4 + 1]#"][fieldnames[i]] = val;
    });
    return byOp;
}
</cfscript>

这可以利用CF在您提及不存在的成员时自动创建结构的事实。

测试:

<cfset v = ListToArray('1,2,3,4,5,6')>
<cfset f = ListToArray('a,b,c,d,e,f')>
<cfoutput>
    <pre>#SerializeJSON(groupByOp(v, f))#</pre>
</cfoutput>

输出

{
    "xsel": {
        "c": "3"
    },
    "xord": {
        "e": "5",
        "a": "1"
    },
    "xsort": {
        "b": "2",
        "f": "6"
    },
    "xmer": {
        "d": "4"
    }
}

答案 1 :(得分:2)

我做了一些CF已经很久了,但是使用local范围的基于标签的方法:

<cfset keys = ['xord', 'xsort', 'xsel', 'xmer'] />
<cfset farray = ['f1','f2','f3','f4','f5','f6','f7','f8']>
<cfset varray = ['v1','v2','v3','v4','v5','v6','v7','v8']>

<cfloop from="1" to="#ArrayLen(varray)#" index="i">
    <cfset local[keys[i%4+1]][farray[i]] = varray[i]>
</cfloop>

<cfdump var="#xord#" />
<cfdump var="#xsort#" />
<cfdump var="#xsel#" />
<cfdump var="#xmer#" />

现在,您xord范围内的xsortxselxmerlocal填充了正确的键值对。

答案 2 :(得分:1)

(你没有提到你的版本,所以我不知道你是否可以访问更新的函数,比如array each()。请记住,在新版本中有更新的选项)

创建一个包含4个变量的单个结构和一个名称数组,而不是创建单独的变量。然后使用数组和MOD填充子结构。 注意,下面的示例预先创建子子结构以确保它们始终存在 - 即使字段/值数组为空或包含少于4个元素。

TryCF.com Example

<cfset farray = ["A","B","C","D","E","F","G","Q","T"]>
<cfset varray = ["11","22","33","RR","55","NN","77","68","46"]>
<cfset data   = {xOrd={},xSort={},xSel={},xMer={}}>
<cfset names  = ["xOrd","xSort","xSel","xMer"]>

<cfloop from="1" to="#ArrayLen(varray)#" index="i">
  <cfset fieldName = farray[i]>
  <cfset fieldValue = varray[i]> 
  <cfset structName = names[i%4+1]>
  <cfset data[structName][fieldName] = fieldValue>
</cfloop>  

可以通过父data

访问子结构
<cfdump var="#data.xOrd#" label="data.xOrd">
<cfdump var="#data.xSort#" label="data.xSort">
<cfdump var="#data.xSel#" label="data.xSel">
<cfdump var="#data.xMer#" label="data.xMer"> 

答案 3 :(得分:0)

另外两种方法。 我不一定会说这些更好,但只是不同:

要求字段数组长度与值数组长度匹配

Function of Parent Class 

OR:

<cfset farray = ['field1','field2','field3','field4','field5']>
<cfset varray = ['apple','orange','pear','kiwi','pineapple']>

<cfset xord  = {}>
<cfset xsort = {}>
<cfset xsel  = {}>
<cfset xmer  = {}>

<cfloop from="1" to="#ArrayLen(varray)#" index="i">
 <cfset j = 
    ((i%4 EQ 1) ? (StructInsert(xord,farray[i],varray[i])):
    ((i%4 EQ 2) ? (StructInsert(xsort,farray[i],varray[i])):
    ((i%4 EQ 3) ? (StructInsert(xsel,farray[i],varray[i])):
    ((i%4 EQ 0) ? (StructInsert(xmer,farray[i],varray[i])): 0))))>
</cfloop>  

<cfdump var="#xord#" />
<cfdump var="#xsort#" />
<cfdump var="#xsel#" />
<cfdump var="#xmer#" />

字段数组长度不必与值数组长度匹配

<cfloop from="1" to="#ArrayLen(varray)#" index="i">
  <cfswitch expression="#i%4#">
    <cfcase value="0">
      <cfset xmer[farray[i]] = varray[i]>
    </cfcase>
    <cfcase value="1">
      <cfset xord[farray[i]] = varray[i]>
    </cfcase>
    <cfcase value="2">
      <cfset xsort[farray[i]] = varray[i]>
    </cfcase>
    <cfcase value="3">
      <cfset xsel[farray[i]] = varray[i]>
    </cfcase>
  </cfswitch>
</cfloop>  

<cfdump var="#xord#" />
<cfdump var="#xsort#" />
<cfdump var="#xsel#" />
<cfdump var="#xmer#" />

OR:

<cfset farray = ['field1','field2','field3','field4']>
<cfset varray = ['apple','orange','pear','kiwi','pineapple']>

<cfset xord  = {}>
<cfset xsort = {}>
<cfset xsel  = {}>
<cfset xmer  = {}>

<cfloop from="1" to="#ArrayLen(varray)#" index="i">
 <cfset j = 
    ((i%4 EQ 1 AND ArrayIsDefined(farray,i)) ? (StructInsert(xord,farray[i],varray[i],true)) :
    ((i%4 EQ 2 AND ArrayIsDefined(farray,i)) ? (StructInsert(xsort,farray[i],varray[i],true)) :
    ((i%4 EQ 3 AND ArrayIsDefined(farray,i)) ? (StructInsert(xsel,farray[i],varray[i],true)) :
    ((i%4 EQ 0 AND ArrayIsDefined(farray,i)) ? (StructInsert(xmer,farray[i],varray[i],true)) : 0))))>
</cfloop>  

<cfdump var="#xord#" />
<cfdump var="#xsort#" />
<cfdump var="#xsel#" />
<cfdump var="#xmer#" />