1

我有以下代码块:

<cfset docs1 = StructNew() />
<cfset docs1['content'] = "I am" />
<cfset docs1['ida'] = "23" />
<cfset docs1['solrid'] = "23_solrid" />
<cfset docs1['title'] = "Aaaaa" />

<cfset docs2 = StructNew() />
<cfset docs2['content'] = "the most" />
<cfset docs2['ida'] = "1" />
<cfset docs2['solrid'] = "1_solrid" />
<cfset docs2['title'] = "Bbbb" />

<cfset docs3 = StructNew() />
<cfset docs3['content'] = "crap coder" />
<cfset docs3['ida'] = "7" />
<cfset docs3['solrid'] = "7_solrid" />
<cfset docs3['title'] = "Cccc" />

<cfset docs4 = StructNew() />
<cfset docs4['content'] = "in the whole universe!" />
<cfset docs4['ida'] = "39" />
<cfset docs4['solrid'] = "39_solrid" />
<cfset docs4['title'] = "Dddd" />

<cfscript> 
    doca = ArrayNew(); 
    ArrayAppend(doca, docs1); 
    ArrayAppend(doca, docs2); 
    ArrayAppend(doca, docs3); 
    ArrayAppend(doca, docs4);  
</cfscript>  

<cfset forsolr1 = StructNew()>
<cfset forsolr1['docs'] = doca>
<cfset forext1 = SerializeJson(forsolr1)>

<cfdump var="#forsolr1#" label="Struct1">
<cfoutput>
    #forext1#
</cfoutput>
<br /><br /><br />


<cfset docsh = StructNew() />
<cfset docsh['23_solrid'] = "I need" />
<cfset docsh['1_solrid'] = "to read" />
<cfset docsh['7_solrid'] = "a lot of" />
<cfset docsh['39_solrid'] = "programming books!" />

<cfset i = 1>
<cfset solrid = "solrid">
<cfset result = ArrayNew(1)>

<cfloop collection="#docsh#" item="key" >
  <cfset innerstruct = StructNew()>
  <cfset innerstruct['solrid'] = #key#>
  <cfset innerstruct['dochighlighted'] = #docsh[key]#>
  <cfset result[i] = innerstruct>
  <cfset i = i + 1>
</cfloop>

<cfset forsolr = StructNew()>
<cfset forsolr['docs'] = result>
<cfset forext = SerializeJson(forsolr)>

<cfdump var="#forsolr#" label="Struct2">
<cfoutput>
    #forext#
</cfoutput>

结果是两个对象(结构数组的结构)

第一个对象

{
   "docs":[
      {
         "content":"I am",
         "ida":"23",
         "solrid":"23_solrid",
         "title":"Aaaaa"
      },
      {
         "content":"the most",
         "ida":"1",
         "solrid":"1_solrid",
         "title":"Bbbb"
      },
      {
         "content":"crap coder",
         "ida":"7",
         "solrid":"7_solrid",
         "title":"Cccc"
      },
      {
         "content":"in the whole universe!",
         "ida":"39",
         "solrid":"39_solrid",
         "title":"Dddd"
      }
   ]
}

第二个对象

{
   "docs":[
      {
         "solrid":"23_solrid",
         "dochighlighted":"I need"
      },
      {
         "solrid":"1_solrid",
         "dochighlighted":"to read"
      },
      {
         "solrid":"7_solrid",
         "dochighlighted":"a lot of"
      },
      {
         "solrid":"39_solrid",
         "dochighlighted":"programming books!"
      }
   ]
}

是否可以将这两个对象合并为一个(将第二个复制到第一个):

{
   "docs":[
      {
         "content":"I am",
         "ida":"23",
         "solrid":"23_solrid",
         "title":"Aaaaa",
         "dochighlighted":"I need"
      },
      {
         "content":"the most",
         "ida":"1",
         "solrid":"1_solrid",
         "title":"Bbbb",
         "dochighlighted":"to read"
      },
      {
         "content":"crap coder",
         "ida":"7",
         "solrid":"7_solrid",
         "title":"Cccc",
         "dochighlighted":"a lot of"
      },
      {
         "content":"in the whole universe!",
         "ida":"39",
         "solrid":"39_solrid",
         "title":"Dddd",
         "dochighlighted":"programming books!"
      }
   ]
}

我试过 structAppend 函数没有结果。此函数不能附加深层嵌套元素。是否可以保留订单?原因是上述对象是搜索引擎的结果,所以保持原始顺序非常重要。是否可以通过键引用合并两个对象?我问的原因是第一个对象以特定顺序从搜索引擎中出现(注意 solrid),但第二个对象可能不是。例子:

 *First object*

{
   "docs":[
      {
         "content":"I am",
         "ida":"23",
         "solrid":"23_solrid",
         "title":"Aaaaa"
      },
      {
         "content":"the most",
         "ida":"1",
         "solrid":"1_solrid",
         "title":"Bbbb"
      },
      {
         "content":"crap coder",
         "ida":"7",
         "solrid":"7_solrid",
         "title":"Cccc"
      },
      {
         "content":"in the whole universe!",
         "ida":"39",
         "solrid":"39_solrid",
         "title":"Dddd"
      }
   ]
}

 *Second object*

{
   "docs":[
      {
         "solrid":"39_solrid",
         "dochighlighted":"programming books!"
      },
      {

         "solrid":"7_solrid",
         "dochighlighted":"a lot of"
      },
      {
         "solrid":"1_solrid",
         "dochighlighted":"to read"
      },
      {
         "solrid":"23_solrid",
         "dochighlighted":"I need"
      }
   ]
}

是否有可能实现与上述合并结果相同的结果?

{
   "docs":[
      {
         "content":"I am",
         "ida":"23",
         "solrid":"23_solrid",
         "title":"Aaaaa",
         "dochighlighted":"I need"
      },
      {
         "content":"the most",
         "ida":"1",
         "solrid":"1_solrid",
         "title":"Bbbb",
         "dochighlighted":"to read"
      },
      {
         "content":"crap coder",
         "ida":"7",
         "solrid":"7_solrid",
         "title":"Cccc",
         "dochighlighted":"a lot of"
      },
      {
         "content":"in the whole universe!",
         "ida":"39",
         "solrid":"39_solrid",
         "title":"Dddd",
         "dochighlighted":"programming books!"
      }
   ]
}

非常感谢,

带着荣誉,

汤姆

希腊

4

1 回答 1

2

这应该让你走得很远:

<cffunction name="ArrayOfStructsMerge" returntype="array" access="public" output="yes">
  <cfargument name="left"  type="array"  required="yes">
  <cfargument name="right" type="array"  required="yes">
  <cfargument name="id"    type="string" required="yes">

  <cfset var result = Duplicate(arguments.left)>
  <cfset var element = "">
  <cfset var key = "">
  <cfset var currentId = "">
  <cfset var lookup = StructNew()>

  <cfloop array="#result#" index="element">
    <cfif IsStruct(element) and StructKeyExists(element, arguments.id)>
      <cfset currentId = element[arguments.id]>
      <cfset lookup[currentId] = element>
    </cfif>
  </cfloop>

  <cfloop array="#arguments.right#" index="element">
    <cfif IsStruct(element) and StructKeyExists(element, arguments.id)>
      <cfset currentId = element[arguments.id]>
      <cfif StructKeyExists(lookup, currentId)>
        <cfloop collection="#element#" item="key">
          <cfset lookup[currentId][key] = Duplicate(element[key])>
        </cfloop>
      <cfelse>
        <cfset ArrayAppend(result, Duplicate(element))>
      </cfif>
    </cfif>
  </cfloop>

  <cfreturn result>
</cffunction>

它以非破坏性方式运行(不修改原始对象)。像这样称呼它:

<cfset result = ArrayOfStructsMerge(obj1.docs, obj2.docs, "solrid")>
<cfdump var="#result#">

<cfset a1 = [{id: 1, a: "a1"}, {id: 2, a: "a2"}, {id: 4, d: "d4"}]>
<cfset a2 = [{id: 1, a: "a1-new"}, {id: 2, b: "b2"}, {id: 3, c: "c3"}]>
<cfset a3 = ArrayOfStructsMerge(a1, a2, "id")>

ArrayOfStructsMerge() 的结果

于 2012-08-05T20:40:41.457 回答