ListRemoveDuplicates 结果末尾有一个逗号

ListRemoveDuplicates result has a comma at the end

在 Lucee 中使用 listRemoveDuplicates 时,它会删除重复值,但仍会在值的末尾保留分隔符。

例如:

<cfset myUsers = 'sathish,sathish'>
<cfset removeDups = listRemoveDuplicates(myUsers)>

<cfdump var="#removeDups#" />

这会产生输出:

sathish, 

但是,当用 Adob​​e ColdFusion 检查这个相同的函数时,它产生了正确的值(末尾没有逗号):

sathish

如何在 Lucee 中省略逗号分隔符?

浏览 Lucee 文档,我发现 ListCompact() 函数 returns 是正确的值。

<cfset myUsers = 'sathish,sathish'>
<cfset removeDups = listCompact(listRemoveDuplicates(myUsers))>
<cfdump var="#removeDups#" />

您还可以像这样创建自定义函数:

<cffunction name="UDF_listRemoveDuplicates" access="public">
    <cfargument name="list" type="string" required="yes">

    <!--- Hash Map to maintain the order --->
    <cfset local.hashMap = createObject("java", "java.util.LinkedHashMap").init()>

    <!--- Add list values as key to Hash Map --->
    <cfloop list="#arguments.list#" index="local.value" delimiters=",">
        <cfset local.hashMap[local.value] = 1>
    </cfloop>

    <!--- Return list of Keys --->
    <cfreturn structKeyList(local.hashMap, ",")>
</cffunction>

<cfoutput>#UDF_listRemoveDuplicates("sathish,sathish")#</cfoutput>

这应该适用于两者。 (未在 Lucee 上测试)