2012-02-20 42 views
1

我有一個嵌套在另一個結構內的數組內的結構,如下所示:Arguments.cart.data.Items[x].Labels.Pkg.Titlex是一個索引,因爲我在循環遍歷Items)。搜索嵌套在一個數組中的結構

Items是一個數組,而Labels,PkgTitle是嵌套結構。

Title並不總是存在。所以我想檢查它。但是,使用structFindKey返回一個錯誤

您試圖取消引用類型的類coldfusion.runtime.Array的標量變量與成員的結構

我只能往裏Arguments.cart.data;但是,如果數組中有多行,某些行可能包含Title,而其他行則不包含。所以我想檢查每個Items裏面的Title

我也試過arrayFind,但後來我得到的錯誤

結構不能被用作陣列

我不知所措就在這裏。

回答

4

這將做的工作

<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不能很好地使用數組玩,而是由陣列中的每個元素分配到臨時值,你就能夠引用它內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> 
1

我以前也遇到過這個。只需將你的數組暫時存入一個結構體中......這將使structFindKey()structFindValue()正常工作。