如何从 Coldfusion 数组中删除重复值?

发布于 2024-11-09 15:20:02 字数 134 浏览 4 评论 0 原文

我有一个接收一串标签的函数。为了单独保存标签,该函数将字符串转换为数组:

this.tags = listToArray(this.tags, ", ");

如果存在重复值,如何删除重复值有吗?

I have a function that receives a string of tags. In order to save the tags individually, the function transforms the string into an array:

this.tags = listToArray(this.tags, ", ");

How do I remove duplicate values in the event that there are any?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

沙与沫 2024-11-16 15:20:02

我喜欢使用 Java 来完成此类任务:

<cfset tags = "apples,oranges,bananas,pears,apples" />

<cfset tagsArray = createObject("java", "java.util.ArrayList").init(
createObject("java", "java.util.HashSet").init(ListToArray(tags))
) />

<cfdump var="#tags#" />
<cfdump var="#tagsArray#" />

唯一的问题是它需要考虑大小写,所以认为“apples”和“apples” “APPLES”是不同的东西(从技术上讲是的,根据您的系统可能会有所不同)。解决方法是首先将列表中的所有内容小写。 (注意:添加了 java.util.ArrayList 函数,以便 Adob​​e ColdFusion 识别并可重用该数组;否则 arraysort 等函数将引发错误。)

I like to use Java for this kind of task:

<cfset tags = "apples,oranges,bananas,pears,apples" />

<cfset tagsArray = createObject("java", "java.util.ArrayList").init(
createObject("java", "java.util.HashSet").init(ListToArray(tags))
) />

<cfdump var="#tags#" />
<cfdump var="#tagsArray#" />

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 like arraysort will throw an error.)

祁梦 2024-11-16 15:20:02

从列表中删除重复项的一个简单方法是首先将列表转换为结构体,然后将结构体转换为数组。但是,如果列表中项目的顺序很重要,则这可能不合适,因为结构中的元素将被排序。

如果项目的顺序很重要,您将需要手动构建数组,而不是使用 listToArray 功能。

<!--- CF9 --->
<cfset tags = "apples,oranges,bananas,pears,APPLES" />
<cfset tagArray = arrayNew(1) />

<cfloop list="#tags#" index="tag" delimiters=",">
    <cfif not ArrayFindNoCase(tagArray,tag)>
        <cfset arrayAppend(tagArray, tag) />
    </cfif>
</cfloop>

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.

<!--- CF9 --->
<cfset tags = "apples,oranges,bananas,pears,APPLES" />
<cfset tagArray = arrayNew(1) />

<cfloop list="#tags#" index="tag" delimiters=",">
    <cfif not ArrayFindNoCase(tagArray,tag)>
        <cfset arrayAppend(tagArray, tag) />
    </cfif>
</cfloop>
软甜啾 2024-11-16 15:20:02

由于您实际上是从字符串/列表开始,然后将其转换为数组,因此您可以通过 ListRemoveDuplicates 。 ListRemoveDuplicates 是在 Coldfusion 10 中引入的;输入参数为 (list, delimiter=",",ignoreCase=FALSE)。

this.tags = listToArray(listRemoveDuplicates(arrayInput,", ",TRUE));

如果您实际上从数组开始,则需要先将其转换为列表,然后再返回。

this.tags = listToArray(listRemoveDuplicates(arrayToList(arrayInput),", ",TRUE) );

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).

this.tags = listToArray(listRemoveDuplicates(arrayInput,", ",TRUE));

If you were actually starting with an array, you would need to convert it to a list first, then back again after.

this.tags = listToArray(listRemoveDuplicates(arrayToList(arrayInput),", ",TRUE) );
回忆追雨的时光 2024-11-16 15:20:02

基于 Jason Haritou 的想法,但您可以使用 Struct 在纯 CF 中完成! (键匹配不区分大小写)

this.tags = listToArray(this.tags, ", ");
var tmpStruct = {};

for (var t in this.tags)
    tmpStruct[t] = "";

return structKeyArray(tmpStruct);

但是,对于小型列表,我更喜欢安东尼的解决方案。

based on idea of Jason Haritou, but you can do it in pure CF using Struct! (keys matching will be case-insensitive)

this.tags = listToArray(this.tags, ", ");
var tmpStruct = {};

for (var t in this.tags)
    tmpStruct[t] = "";

return structKeyArray(tmpStruct);

However, for small lists, I prefer Antony's solution.

拔了角的鹿 2024-11-16 15:20:02

在 Coldfusion 10 或 Railo 4 中,您可以使用 Underscore.cfc 的 uniq() 函数

_ = new Underscore();

uniqueArray = _.uniq(arrayWithDuplicates);

一个优点uniq() 的优点是它允许您在必要时传递转换函数。

注:我写的是Underscore.cfc

In Coldfusion 10 or Railo 4, you could use Underscore.cfc's uniq() function:

_ = new Underscore();

uniqueArray = _.uniq(arrayWithDuplicates);

One advantage of uniq() is that it allows you to pass a transformation function, if necessary.

Note: I wrote Underscore.cfc

猫弦 2024-11-16 15:20:02

我只需要对一个非常大的列表(5k+条目)进行重复数据删除,并找到比使用循环更快的方法。我觉得有必要分享一下。

  1. 将列表转换为数组(您已经有数组,因此跳过)
  2. 使用 queryNew("") 创建查询
  3. 使用第 1 步中的数组将列添加到该查询
  4. 查询 Distinct value SELECT DISTINCT items FROM thisQuery
  5. 将结果转换为列表
  6. 将此列表转换回数组是一个简单的步骤,

我将其写入函数中以方便使用:

<cffunction name="deDupList" output="no" returntype="string">
    <cfargument name="thisList" required="yes">
    <cfargument name="thisDelimeter" required="yes" default=",">
    <cfset var loc = StructNew()>

    <cfset loc.thisArray = ListToArray(thisList,thisDelimeter)>
    <cfset loc.thisQuery = QueryNew("")>
    <cfset loc.temp = QueryAddColumn(loc.thisQuery,"items","varChar",loc.thisArray)>
    <cfquery name="qItems" dbtype="query">
        SELECT DISTINCT items FROM loc.thisQuery
    </cfquery>
    <cfset loc.returnString = ValueList(qItems.items)>
    <cfreturn loc.returnString>
</cffunction>

我将其与其他一些方法进行了基准测试,以下是以毫秒为单位的结果:
循环列表检查 > 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.

  1. convert list to array (you already have array, so skip)<cfset thisArray = ListToArray(thisList)>
  2. Create a query with queryNew("") <cfset thisQuery = QueryNew("")>
  3. Add column to that query with the array from step1 <cfset temp = QueryAddColumn(thisQuery,"items","varChar",thisArray)>
  4. Query for Distinct values <cfquery name="qItems" dbtype="query">SELECT DISTINCT items FROM thisQuery</cfquery>
  5. convert result to list <cfset returnString = ValueList(qItems.items)>
  6. It's an easy step for you to convert this list back to an array

I wrote this into a function for easy use:

<cffunction name="deDupList" output="no" returntype="string">
    <cfargument name="thisList" required="yes">
    <cfargument name="thisDelimeter" required="yes" default=",">
    <cfset var loc = StructNew()>

    <cfset loc.thisArray = ListToArray(thisList,thisDelimeter)>
    <cfset loc.thisQuery = QueryNew("")>
    <cfset loc.temp = QueryAddColumn(loc.thisQuery,"items","varChar",loc.thisArray)>
    <cfquery name="qItems" dbtype="query">
        SELECT DISTINCT items FROM loc.thisQuery
    </cfquery>
    <cfset loc.returnString = ValueList(qItems.items)>
    <cfreturn loc.returnString>
</cffunction>

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

寄人书 2024-11-16 15:20:02

进一步了解杰森的答案,这里是一个 arrayDistinct 函数。

function arrayDistinct (required array data) {
    var output = arrayNew(1);
    output.addAll(createObject("java", "java.util.HashSet").init(arguments.data));
    return output;
}

您可以在这里测试它: https://trycf.com/gist/62ff904d4500519e3144fc9564d2bce7/acf

Taking jason's answer just a little bit further, here is an arrayDistinct function.

function arrayDistinct (required array data) {
    var output = arrayNew(1);
    output.addAll(createObject("java", "java.util.HashSet").init(arguments.data));
    return output;
}

You can test it here: https://trycf.com/gist/62ff904d4500519e3144fc9564d2bce7/acf

清风夜微凉 2024-11-16 15:20:02

只需将数组放入 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

海未深 2024-11-16 15:20:02

我正在发布另一个我喜欢的简洁解决方案。

arrayReduce(arrayWithDuplicates, function(resultArr, item) {
   if(!arrayFind(resultArr, item)){
      arrayAppend(resultArr, item);
   }
   return resultArr;
}, [])

ColdFusion 11 提供了 ArrayReduce。

但是为了更好地回答这个问题,从 ColdFusion 10 开始,我们提供了 ListRemoveDuplicates 功能

所以最终的代码可能如下所示:

this.tags = listToArray(listRemoveDuplicates(this.tags, ", "), ", ");

I'm posting another neat solution I like.

arrayReduce(arrayWithDuplicates, function(resultArr, item) {
   if(!arrayFind(resultArr, item)){
      arrayAppend(resultArr, item);
   }
   return resultArr;
}, [])

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:

this.tags = listToArray(listRemoveDuplicates(this.tags, ", "), ", ");
惟欲睡 2024-11-16 15:20:02

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文