如何从 Coldfusion 数组中删除重复值?
我有一个接收一串标签的函数。为了单独保存标签,该函数将字符串转换为数组:
this.tags = listToArray(this.tags, ", ");
如果存在重复值,如何删除重复值有吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个接收一串标签的函数。为了单独保存标签,该函数将字符串转换为数组:
this.tags = listToArray(this.tags, ", ");
如果存在重复值,如何删除重复值有吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
我喜欢使用 Java 来完成此类任务:
唯一的问题是它需要考虑大小写,所以认为“apples”和“apples” “APPLES”是不同的东西(从技术上讲是的,根据您的系统可能会有所不同)。解决方法是首先将列表中的所有内容小写。 (注意:添加了
java.util.ArrayList
函数,以便 Adobe ColdFusion 识别并可重用该数组;否则arraysort
等函数将引发错误。)I like to use Java for this kind of task:
Only problem is it takes case into account, so thinks "apples" & "APPLES" are different things (which technically yes, depending on your system may well be different). Way round that is to lower case everything in the list first. (NOTE: Added
java.util.ArrayList
function so that the array is identified & reusable by Adobe ColdFusion; otherwise functions likearraysort
will throw an error.)从列表中删除重复项的一个简单方法是首先将列表转换为结构体,然后将结构体转换为数组。但是,如果列表中项目的顺序很重要,则这可能不合适,因为结构中的元素将被排序。
如果项目的顺序很重要,您将需要手动构建数组,而不是使用 listToArray 功能。
An easy way to remove duplicates from a list is to convert the list to a struct first, and then conver the struct to an array. However if the order of items in the list is important this may not be appropriate as the elements in the struct will be sorted.
If the order of items is important you would need to build the array manually rather than using the listToArray feature.
由于您实际上是从字符串/列表开始,然后将其转换为数组,因此您可以通过 ListRemoveDuplicates 。 ListRemoveDuplicates 是在 Coldfusion 10 中引入的;输入参数为 (list, delimiter=",",ignoreCase=FALSE)。
如果您实际上从数组开始,则需要先将其转换为列表,然后再返回。
Since you're really starting with a string/list that you're then converting to an array, you can pass the string through ListRemoveDuplicates before converting the to an array. ListRemoveDuplicates was introduced in Coldfusion 10; the input parameters are (list, delimiter=",", ignoreCase=FALSE).
If you were actually starting with an array, you would need to convert it to a list first, then back again after.
基于 Jason Haritou 的想法,但您可以使用 Struct 在纯 CF 中完成! (键匹配不区分大小写)
但是,对于小型列表,我更喜欢安东尼的解决方案。
based on idea of Jason Haritou, but you can do it in pure CF using Struct! (keys matching will be case-insensitive)
However, for small lists, I prefer Antony's solution.
在 Coldfusion 10 或 Railo 4 中,您可以使用 Underscore.cfc 的 uniq() 函数:
一个优点uniq() 的优点是它允许您在必要时传递转换函数。
注:我写的是Underscore.cfc
In Coldfusion 10 or Railo 4, you could use Underscore.cfc's uniq() function:
One advantage of
uniq()
is that it allows you to pass a transformation function, if necessary.Note: I wrote Underscore.cfc
我只需要对一个非常大的列表(5k+条目)进行重复数据删除,并找到比使用循环更快的方法。我觉得有必要分享一下。
我将其写入函数中以方便使用:
我将其与其他一些方法进行了基准测试,以下是以毫秒为单位的结果:
循环列表检查 > 1 个实例:6265
使用亨利的结构方法:2969
上述方法:31
杰森的方法:30
I just had to de-dup a very large list (5k+entries) and found a much faster way than using a loop. I feel the need to share.
<cfset thisArray = ListToArray(thisList)>
<cfset thisQuery = QueryNew("")>
<cfset temp = QueryAddColumn(thisQuery,"items","varChar",thisArray)>
<cfquery name="qItems" dbtype="query">SELECT DISTINCT items FROM thisQuery</cfquery>
<cfset returnString = ValueList(qItems.items)>
I wrote this into a function for easy use:
I bench-marked it against a few other methods and here are the results in milliseconds:
Looping over List checking for > 1 instance: 6265
Using Henry's struct method: 2969
The above method: 31
Jason's Method: 30
进一步了解杰森的答案,这里是一个 arrayDistinct 函数。
您可以在这里测试它: https://trycf.com/gist/62ff904d4500519e3144fc9564d2bce7/acf
Taking jason's answer just a little bit further, here is an
arrayDistinct
function.You can test it here: https://trycf.com/gist/62ff904d4500519e3144fc9564d2bce7/acf
只需将数组放入 Struct 中,然后将其复制回数组即可;)
http://www.bennadel.com/blog/432-Using-ColdFusion-Structures-To-Remove-Duplicate-List-Values.htm
Just put the array into a Struct and then copy it back to an array ;)
http://www.bennadel.com/blog/432-Using-ColdFusion-Structures-To-Remove-Duplicate-List-Values.htm
我正在发布另一个我喜欢的简洁解决方案。
ColdFusion 11 提供了 ArrayReduce。
但是为了更好地回答这个问题,从 ColdFusion 10 开始,我们提供了
ListRemoveDuplicates
功能。所以最终的代码可能如下所示:
I'm posting another neat solution I like.
ArrayReduce
is available from ColdFusion 11.But to answer better for the question, from the ColdFusion 10, we have
ListRemoveDuplicates
function available.So the final code may looks like this:
CFLib 上有几个 UDF 可以执行此操作:ArrayyDiff (http://www.cflib.org/udf/arrayDiff) 和 ArrayCompare (http://www.cflib.org/udf/arrayCompare)。
哈,
拉里
There are a couple of UDF's on CFLib that do this, ArrayyDiff (http://www.cflib.org/udf/arrayDiff) and ArrayCompare (http://www.cflib.org/udf/arrayCompare).
hth,
larry