AS3遍历/查找深层嵌套对象的最佳方法

时间:2011-11-28 11:19:15

标签: actionscript-3

我有一个需要查找和检索dep嵌套对象的函数,并且想知道是否有一些优化函数来执行它。 在E4X / XML中,我们有像contains或descendants这样的函数。我找不到原生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"

任何想法?

任何帮助都超过了预期!!

1 个答案:

答案 0 :(得分:4)

as3已经有很多深度跟踪工具类。这是一个简单的片段,也许这对你有帮助:

// 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));