开发者

what is the best way to test if a struct exists before inserting it in an array?

This is about coldfusion...

I have an array of structs in which I would like to get only dictinct 开发者_JS百科values. What is the best way to test if a structure already exists in my array before inserting this one ? Should it be possible to test this with contains(javacast(...)) ?

Thank you in advance, Michel

Finally I found how to add a portion of code :)

<cfset count = 0>
<cfset foo = []>
<cfif this struct does not exists in the array...>
    <cfset count = count + 1>
    <cfset foo[count] = {key = currentBar.getValue(), value = anotherValueVar}
</cfif>

Hope this will help...

Thank you, Michel


CF9:

if (!ArrayContains(structs, struct))
    arrayAppend(structs, struct);

CF8, try Java's contains() in java.util.List. CF array extends java.util.Vector so I guess this will work:

if (structs.contains(struct))
    arrayAppend(foo, struct);


Instead of using an array of structs, use a query, and then do a select distinct query-of-queries on it.

<cfscript>
    q = QueryNew('key,value');

    // add a row to the query
    QueryAddRow(q, 1);
    QuerySetCell(q, 'key', currentBar.getValue());
    QuerySetCell(q, 'value', anotherValueVar);

</cfscript>
<!--- Now that all the (non-distinct) rows have been added. --->
<cfquery name="dq" dbtype="query">
    <!--- Have to escape the names "key" and "value" with brackets
          because they are reserved words in CF queries. --->
    select distinct [key], [value] from q
</cfquery>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜