coldfusion distinct list

我想知道是否有一种简单的方法可以在coldfusion列表或数组中强制执行不同的值。 谢谢     
已邀请:
没有预定义的函数可以满足您的要求,但很容易实现自己的功能。我提供的功能非常简单,易于扩展。
variables.myList = "one,two,three";
variables.myList = ListAppendDistinct(variables.myList, "three");
variables.myList = ListAppendDistinct(variables.myList, "four");

function ListAppendDistinct(list, value)
{
    var _local = StructNew();
    _local.list = list;
    if (NOT ListContains(_local.list, value))
    {
        _local.list = ListAppend(_local.list,value);
    }
    return _local.list;
}
您可以使用上面的函数明确附加到数组,这都假设您使用默认分隔符。我不确定您的数据的“大小”,因为它可能会变得昂贵。
variables.myArray = ArrayNew(1);
variables.myArray[1] = "one";
variables.myArray[2] = "two";
variables.myArray[3] = "three";

variables.myArray = ArrayAppendDistinct(variables.myArray, "three");
variables.myArray = ArrayAppendDistinct(variables.myArray, "four");

function ArrayAppendDistinct(array, value)
{
    var _local = StructNew();
    _local.list = ArrayToList(array);
    _local.list = ListAppendDistinct(_local.list,value);
    return ListToArray(_local.list);
}
    
<cfset temp = structNew()>
<cfloop list="a,b,c,a,c" index="i">
  <cfset temp[i] = "">
</cfloop>
<cfset distinctList = structKeyList(temp)>
这是我能想到的最简单的解决方案。缺点是订单未保留,列表项不区分大小写。如果需要不区分大小写,请使用Java的hashset。     
在添加值之前,使用arrayContains或listFindNoCase检查它是否存在。     
您可以在CF 10或Railo 4中使用Underscore.cfc库:
_ = new Underscore();// instantiate the library    
uniqueArray = _.uniq(array);// convert an array to a unique array
我不认为它比这更简单! (免责声明:我写过Underscore.cfc)     
对于那些现在正在看这个答案的人:是的。有一个叫做
ListRemoveDuplicates()
的功能。它被添加到ColdFusion 10(2017)中。 例如,下面的代码返回唯一值“AA,BB,CC”
newList = listRemoveDuplicates("AA,BB,CC,AA,AA,AA,BB", ",");
writeOutput("newList = #newList#");
    

要回复问题请先登录注册