List 汇总列表中的动态字段
我有这样一个清单:List 汇总列表中的动态字段,list,coldfusion,lucee,List,Coldfusion,Lucee,我有这样一个清单: <cfset list ="group 1:1; group 2:4; group a:7; group 1:3; group a:1;"> 所以这个列表只是一个例子,事实上,它更大,有更多的组(动态名称)。我用的是Coldfusion/Lucee。有人能帮我吗?这只是众多可能的替代方法之一。从使用查询而不是列表作为起始值开始 我所做的是用循环列表作为分隔符,并将值添加到结构中。我稍后使用该结构循环并列出最终的总数 <cfset lis
<cfset list ="group 1:1; group 2:4; group a:7; group 1:3; group a:1;">
所以这个列表只是一个例子,事实上,它更大,有更多的组(动态名称)。我用的是Coldfusion/Lucee。有人能帮我吗?这只是众多可能的替代方法之一。从使用查询而不是列表作为起始值开始 我所做的是用
循环列表代码>作为分隔符,并将值添加到结构中。我稍后使用该结构循环并列出最终的总数
<cfset list ="group 1:1; group 2:4; group a:7; group 1:3; group a:1;">
<cfset totalStruct = {}>
<cfloop list="#list#" item="group" delimiters=';'>
<cfset groupName = listFirst(group, ':')>
<cfset groupNameKey = replace(groupName, ' ', '', 'all')>
<cfset groupValue = val(listLast(group, ':'))>
<cfif !structKeyExists(totalStruct, groupNameKey)>
<cfset totalStruct[groupNameKey] = {name:groupName, total=groupValue}>
<cfelse>
<cfset totalStruct[groupNameKey].total += groupValue>
</cfif>
</cfloop>
<cfoutput>
<ul>
<cfloop collection="#totalStruct#" item="group">
<li>#totalStruct[group].name# : #totalStruct[group].total#</li>
</cfloop>
</ul>
</cfoutput>
- #totalStruct[group]。名称:#totalStruct[group]。总计#
另一种选择是使用map
和/或reduce
闭包函数来解析列表
注1:我通常发现cfscript对于解析文本或“循环”的大多数事情来说都要容易得多
注2:我不太喜欢循环,尤其是大文本值的循环。闭包函数的性能可能会更好
然后使用reduce()
,而不在函数内部
<cfscript>
//// https://docs.lucee.org/reference/functions/listreduce.html
r = listReduce(lst,
function(prev,nxt){
k = nxt.listFirst(":").ltrim() ; /// Get the "key" and strip extra leading space.
/// To shorten it, I just skipped setting the value beforehand and just did it while setting the struct value. Same method as above.
prev["#k#"] = (nxt.listLast(":"))+(prev["#k#"]?:0) ;
return prev ;
}
,
{} // Initial value
,";" // Delimiter
) ;
writedump(r) ;
</cfscript>
<cfset lst ="group 1:1; group 2:4; group a:7; group 1:3; group a:1;">
<cfscript>
public Struct function parseLst (required String lst) {
var retval = {} ; //// Default return variable.
//// https://docs.lucee.org/reference/functions/listmap.html
arguments.lst.listmap(
function(el) {
var k = el.listFirst(":").ltrim() ; /// Get the "key" and strip extra leading space.
var v = el.listLast(":") ; /// Get the "value".
var p = retval["#k#"]?:0 ; /// Use Elvis to default to 0 if no struct Key exists.
retval["#k#"] = v + p ; /// Set the value of the key. NOTE: A struck key with the same name will generally overwrite itself. We want to add it.
}
,";" /// Specify the delimiter of the list.
) ;
return retval ;
}
writeDump(parseLst(lst));
</cfscript>
<cfscript>
//// https://docs.lucee.org/reference/functions/listreduce.html
r = listReduce(lst,
function(prev,nxt){
k = nxt.listFirst(":").ltrim() ; /// Get the "key" and strip extra leading space.
/// To shorten it, I just skipped setting the value beforehand and just did it while setting the struct value. Same method as above.
prev["#k#"] = (nxt.listLast(":"))+(prev["#k#"]?:0) ;
return prev ;
}
,
{} // Initial value
,";" // Delimiter
) ;
writedump(r) ;
</cfscript>
<cfscript>
//// https://docs.lucee.org/reference/functions/listreduce.html
s = listReduce(lst,
function(prev,nxt){
var elem = nxt.listToArray(":") ;
prev["#elem[1].ltrim()#"] = elem[2] + (prev["#elem[1].ltrim()#"]?:0) ;
return prev ;
}
,
{} // Initial value
,";" // Delimiter
) ;
writedump(s) ;
</cfscript>