Tim*_*hle 9 arrays coldfusion list distinct distinct-values
我想知道是否有一种简单的方法可以在coldfusion列表或数组中强制执行不同的值.
谢谢
Hen*_*nry 10
<cfset temp = structNew()>
<cfloop list="a,b,c,a,c" index="i">
<cfset temp[i] = "">
</cfloop>
<cfset distinctList = structKeyList(temp)>
Run Code Online (Sandbox Code Playgroud)
这是我能想到的最简单的解决方案.缺点是订单未保留,列表项不区分大小写.如果需要不区分大小写,请使用Java的hashset.
没有预定义的函数可以执行您所要求的操作,但很容易实现您自己的函数来执行此操作。我提供的功能非常简单并且易于扩展。
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;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用上面的函数明确附加到数组,这一切都假设您使用默认分隔符。我不确定您的数据的“大小”,因为它可能会变得昂贵。
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3961 次 |
| 最近记录: |