Arrays 搜索嵌套在数组中的结构

Arrays 搜索嵌套在数组中的结构,arrays,coldfusion,struct,Arrays,Coldfusion,Struct,我有一个嵌套在另一个结构中的数组中的结构,比如:Arguments.cart.data.Items[x].Labels.Pkg.Title x是一个索引,因为我在项目上循环 Items是一个数组,而Label、Pkg和Title是嵌套结构 标题并不总是存在。所以我想检查一下。但是,使用structFindKey会返回一个错误 您已尝试将coldfusion.runtime.Array类型的标量变量作为具有成员的结构取消引用 我可以只查看Arguments.cart.data内部;但是,如果数组中

我有一个嵌套在另一个结构中的数组中的结构,比如:Arguments.cart.data.Items[x].Labels.Pkg.Title x是一个索引,因为我在项目上循环

Items是一个数组,而Label、Pkg和Title是嵌套结构

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

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

我可以只查看Arguments.cart.data内部;但是,如果数组中有多行,则某些行可能包含标题,而其他行可能不包含标题。所以我想检查每个项目中的标题

我也尝试了arrayFind,但后来我发现了错误

结构不能用作数组


我在这里不知所措

我过去也遇到过这种情况。只需将数组临时粘贴到结构中。。。这将诱使structFindKey和structFindValue正常工作。

我过去也遇到过这种情况。只需将数组临时粘贴到结构中。。。这将诱使structFindKey和structFindValue正常工作。

这将完成工作

<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中引用它

或者,如果您愿意,也可以执行以下操作

<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>
这就行了

<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中引用它

或者,如果您愿意,也可以执行以下操作

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