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

时间:2021-03-08 07:29:21

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, ", ");

这一点。标签= listToArray(这一点。标签,",");

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

如何删除事件中存在的重复值?

9 个解决方案

#1


9  

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.

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

If the order of items is important you would need to build the array manually rather than using the listToArray feature.

如果项的顺序很重要,那么需要手工构建数组,而不是使用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>

#2


17  

I like to use Java for this kind of task:

我喜欢在这类任务中使用Java:

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

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

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

唯一的问题是它考虑了案例,所以认为“苹果”和“苹果”是不同的东西(技术上是不同的,这取决于你的系统)。方法是先把列表中的所有项都降低。

#3


4  

return listToArray(listRemoveDuplicates(arrayToList(arrayInput)));

#4


2  

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

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

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.

然而,对于小名单,我更喜欢安东尼的解决方案。

#5


2  

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.

我只需去除了一个非常大的列表(5k+条目),并找到了一种比使用循环快得多的方法。我觉得有必要分享。

  1. convert list to array (you already have array, so skip)<cfset thisArray = ListToArray(thisList)>
  2. 将列表转换为数组(您已经有了数组,所以跳过)
  3. Create a query with queryNew("") <cfset thisQuery = QueryNew("")>
  4. 使用queryNew("") 创建查询
  5. Add column to that query with the array from step1 <cfset temp = QueryAddColumn(thisQuery,"items","varChar",thisArray)>
  6. 从step1 添加列到该查询。
  7. Query for Distinct values <cfquery name="qItems" dbtype="query">SELECT DISTINCT items FROM thisQuery</cfquery>
  8. 查询不同的值 从这个查询 中选择不同的项
  9. convert result to list <cfset returnString = ValueList(qItems.items)>
  10. 将结果转换为list
  11. It's an easy step for you to convert this list back to an array
  12. 将这个列表转换回数组是一个简单的步骤

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

我用其他一些方法标记它,这里是以毫秒为单位的结果:循环检查> 1实例的列表:6265使用Henry的struct方法:2969上面的方法:31 Jason的方法:30

#6


1  

Just put the array into a Struct and then copy it back to an array ;)

只需将数组放入Struct中,然后将其复制回数组;)

http://www.bennadel.com/blog/432-Using-ColdFusion-Structures-To-Remove-Duplicate-List-Values.htm

http://www.bennadel.com/blog/432-Using-ColdFusion-Structures-To-Remove-Duplicate-List-Values.htm

#7


1  

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

在Coldfusion 10或Railo 4中,可以使用下划线。氯氟化碳的uniq()函数:

_ = new Underscore();

uniqueArray = _.uniq(arrayWithDuplicates);

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

uniq()的一个优点是,如果需要,它允许您传递一个转换函数。

Note: I wrote Underscore.cfc

注意:我Underscore.cfc写道

#8


1  

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

让杰森的答案再深入一点,这是一个arrayDistinct函数。

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

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

#9


0  

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

在CFLib上有几个UDF这样做,ArrayyDiff (http://www.cflib.org/udf/arrayDiff)和ArrayCompare (http://www.cflib.org/udf/arrayCompare)。

hth, larry

hth,拉里

#1


9  

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.

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

If the order of items is important you would need to build the array manually rather than using the listToArray feature.

如果项的顺序很重要,那么需要手工构建数组,而不是使用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>

#2


17  

I like to use Java for this kind of task:

我喜欢在这类任务中使用Java:

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

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

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

唯一的问题是它考虑了案例,所以认为“苹果”和“苹果”是不同的东西(技术上是不同的,这取决于你的系统)。方法是先把列表中的所有项都降低。

#3


4  

return listToArray(listRemoveDuplicates(arrayToList(arrayInput)));

#4


2  

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

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

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.

然而,对于小名单,我更喜欢安东尼的解决方案。

#5


2  

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.

我只需去除了一个非常大的列表(5k+条目),并找到了一种比使用循环快得多的方法。我觉得有必要分享。

  1. convert list to array (you already have array, so skip)<cfset thisArray = ListToArray(thisList)>
  2. 将列表转换为数组(您已经有了数组,所以跳过)
  3. Create a query with queryNew("") <cfset thisQuery = QueryNew("")>
  4. 使用queryNew("") 创建查询
  5. Add column to that query with the array from step1 <cfset temp = QueryAddColumn(thisQuery,"items","varChar",thisArray)>
  6. 从step1 添加列到该查询。
  7. Query for Distinct values <cfquery name="qItems" dbtype="query">SELECT DISTINCT items FROM thisQuery</cfquery>
  8. 查询不同的值 从这个查询 中选择不同的项
  9. convert result to list <cfset returnString = ValueList(qItems.items)>
  10. 将结果转换为list
  11. It's an easy step for you to convert this list back to an array
  12. 将这个列表转换回数组是一个简单的步骤

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

我用其他一些方法标记它,这里是以毫秒为单位的结果:循环检查> 1实例的列表:6265使用Henry的struct方法:2969上面的方法:31 Jason的方法:30

#6


1  

Just put the array into a Struct and then copy it back to an array ;)

只需将数组放入Struct中,然后将其复制回数组;)

http://www.bennadel.com/blog/432-Using-ColdFusion-Structures-To-Remove-Duplicate-List-Values.htm

http://www.bennadel.com/blog/432-Using-ColdFusion-Structures-To-Remove-Duplicate-List-Values.htm

#7


1  

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

在Coldfusion 10或Railo 4中,可以使用下划线。氯氟化碳的uniq()函数:

_ = new Underscore();

uniqueArray = _.uniq(arrayWithDuplicates);

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

uniq()的一个优点是,如果需要,它允许您传递一个转换函数。

Note: I wrote Underscore.cfc

注意:我Underscore.cfc写道

#8


1  

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

让杰森的答案再深入一点,这是一个arrayDistinct函数。

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

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

#9


0  

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

在CFLib上有几个UDF这样做,ArrayyDiff (http://www.cflib.org/udf/arrayDiff)和ArrayCompare (http://www.cflib.org/udf/arrayCompare)。

hth, larry

hth,拉里