【问题标题】:Search a struct nested in an array搜索嵌套在数组中的结构
【发布时间】:2012-02-20 17:08:09
【问题描述】:

我有一个结构体,它位于嵌套在另一个结构体中的数组中,如下所示:Arguments.cart.data.Items[x].Labels.Pkg.Titlex 是一个索引,因为我正在循环遍历Items)。

Items 是一个数组,而 LabelsPkgTitle 是嵌套结构。

Title 并不总是存在。所以我想检查一下。但是,使用structFindKey 会返回错误

您试图取消引用类型为coldfusion.runtime.Array 类型的标量变量作为具有成员的结构

我可以看看Arguments.cart.data;但是,如果数组中有多行,则某些行可能包含Title,而其他行则不包含。所以我想在每个Items 中检查Title

我也试过arrayFind,但后来我得到了错误

结构体不能作为数组使用

我在这里不知所措。

【问题讨论】:

    标签: arrays coldfusion struct


    【解决方案1】:

    这样就可以了

    <cfscript>
        for (i=1;i<=ArrayLen(arguments.cart.data.Items);i++) {
            tempI = arguments.cart.data.Items[i];
            if (IsDefined('tempI.Labels.Pkg.Title')) {
                // It exists
            } else {
                // It doesn't
            }
        }
    </cfscript>
    

    IsDefined 不能很好地处理数组,但是通过将数组的每个元素分配给 temp 值,您就可以在 IsDefined 中引用它。

    如果您更喜欢 StructKeyExists,您也可以执行以下操作

    <cfscript>
        for (i=1;i<=ArrayLen(arguments.cart.data.Items);i++) {
            tempI = arguments.cart.data.Items[i];
            if (
                StructKeyExists(tempI,'Labels')
                && StructKeyExists(tempI.Labels,'Pkg')
                && StructKeyExists(tempI.Labels.Pkg,'Title')
            ) {
                // It exists
            } else {
                // It doesn't
            }
        }
    </cfscript>
    

    【讨论】:

      【解决方案2】:

      我过去也遇到过这种情况。只需将您的数组临时粘贴到结构中...这将欺骗 structFindKey()structFindValue() 正常工作。

      【讨论】:

        猜你喜欢
        • 2016-10-29
        • 2016-01-06
        • 1970-01-01
        • 2016-12-08
        • 1970-01-01
        • 2013-01-18
        • 1970-01-01
        • 2015-08-14
        • 1970-01-01
        相关资源
        最近更新 更多