2011-11-28 64 views
1

我有一個函數需要找到並檢索dep嵌套對象,並想知道是否有一些優化的函數來做到這一點。 在E4X/XML中,我們有像包含或後代的函數。我無法找到任何本地AS3對象。對象的AS3遍歷/找到深嵌套對象的最佳方式

一個例子是這樣的:

[56] (Object)#2262 
ID = "Story_u1cac" 
Story = (Array)#2263 
    [0] (Object)#2264 
    AppliedNamedGrid = "n" 
    AppliedTOCStyle = "n" 
    InCopyExportOption = (Array)#2265 
     [0] (Object)#2266 
     IncludeAllResources = false 
     IncludeGraphicProxies = true 
    Self = "u1cac" 
    StoryPreference = (Array)#2267 
     [0] (Object)#2268 
     FrameType = "TextFrameType" 
     OpticalMarginAlignment = true 
     OpticalMarginSize = 12 
     StoryDirection = "LeftToRightDirection" 
     StoryOrientation = "Horizontal" 
    StoryTitle = "$ID/" 
    TrackChanges = false 
    XMLElement = (Array)#2269 
     [0] (Object)#2270 
     MarkupTag = "XMLTag/mehr_1_story" 
     ParagraphStyleRange = (Array)#2271 
      [0] (Object)#2272 
      AppliedParagraphStyle = "ParagraphStyle/Lauftexte%3ainterview" 
      CharacterStyleRange = (Array)#2273 
       [0] (Object)#2274 
       AppliedCharacterStyle = "CharacterStyle/$ID/[No character style]" 
       BaselineShift = 4 
       Content = (Array)#2275 
        [0] (Object)#2276 
        nodeValue = "p" 

什麼想法?

任何幫助是超過apreciated!

回答

1

因此,使用由Mark Knol的上面貼的方法我能箱子一種深深的對象過濾器。 代碼如下:

function filterObj(object:*, filter:String, arr:Array = null):Array { 
     if(arr == null) arr = new Array; 

     for(var item:* in object) { 
      if(object[item] is Array){ 
       for(var i:int = 0; i < object[item].length; i++){ 
        if(item == filter) { 
         arr.push("some property on some deep nested object"); 
        } 
        filterObj(object[item][i], filter, arr); 
       } 
      }else{ 
       filterObj(object[item], filter, arr); 
      } 
     } 
     return arr; 
    } 

用這種方法I'm能夠遍歷任何對象樹並推動所有需要的特性。 的缺點是,如果我推的對象,顯然嵌套對象會在家長中產生複印件上的開銷......

希望它能幫助:)

4

as3已經有很多深度跟蹤util類。下面是一個簡單的片段,也許這可以幫助你:

// some testdata object 
var testData:Object = {myObject:{yourVar:"level1"},testObject:"root level",anotherObject:{nestedC:{myVar:"level 2"},greatObject:{otherVar:3,nestedD:{anotherVar:"level3"}}}}; 

function logDeep(object:*, level:uint = 0):String { 
    var retval:String = ""; 
    for(var item:* in object) { 
     if (typeof(object[item]) == "object") { 
      retval += getTabs(level) + "[" + level + "]: " + item + " (" + typeof(object[item]) + ")\n"; 
      retval += logDeep(object[item], level + 1); 
     } else { 
      if (typeof(object[item]) != "string") retval += getTabs(level) + " - " + item + " : " + object[item] + " (" + typeof(object[item]) + ")\n"; 
      else retval += getTabs(level) + " - " + item + " : '" + object[item] + "' (" + typeof(object[item]) + ")\n"; 
     } 
    } 
    return retval; 
} 

function getTabs(level:uint = 0):String { 
    var retval:String = ""; 
    while (level--) retval+= "\t" 
    return retval; 
} 

// output it to trace-window. 
trace(logDeep(testData)); 
+0

嘿馬克!謝謝你的片段!我想從主對象中提取對象,如果它們符合某些標準的話......我稍微改變了你的函數並做了一個過濾器。我不能發表評論,所以我會作爲答覆發佈......順便說一句,我是你的工作和你的博客的粉絲!感謝發電機;) – Drala