2012-03-09 105 views
0

所以這裏是我的場景 - 我正在收集一個動態文件夾結構與JavaScript爲我開發的一個photoshop插件。以下是文件夾可能是什麼樣子:數組的陣列?

Folder 1 
    subFolder1 
     file1 
     file2 
    subFolder2 
     subsubfolder1 
     subsubfolder2 
      file1 
      file2 


Folder 2 
     subFolder1 
      file1 
      file2 
     subFolder2 
      subsubfolder1 
      subsubfolder2 
       file1 
       file2 

將這個語法是什麼樣的?試圖想出來,但看起來並不正確。另外你將如何循環這樣的事情?你看看每個子陣列嗎?

var multiArray = [ [Folder1, [subFolder1, [file1,file2],subFolder2, [subsubFolder1, subsubFolder2, [file1,file2] ], Folder2, [subFolder1, [file1,file2], subFolder2, [subsubfolder1,subsubfoler2, [file1,file2] 

回答

4
var who = { 
    just: "an object", 
    with: "a couple of properties" 
}; 

var ArrayOofArrays = [ 
    [1,2,3], 
    [4,5,7], 
    [1,{},""], 
    ["a",2,{c: "b",e: who}] 
]; 

在您的例子:

var folder = { 
    subfolders: [ 
     subfolder, 
     subfolder 
    ], 
    files: [ 
     file, 
     file 
    ] 
}; 

子文件夾將是 - >

var subfolder = function() { 
    return { 
     folders: [ 
     ], 
     files: [ 
     ], 
     path: "path/to/folder" // etc.. 
    } 
}; 

文件將是 - >

var fileInfo = function() { 
    return { 
     filename: "path/to/file" // etc. 
    }; 
} 

它確實取決於你想要達到的目標,但通常情況下,對象和數組的組合是最好的。

,所以你可以有類似

var buildFolder = function(){ 
    return { 
     folders: [], 
     files: [] 
    }; 
}; 

var buildFile = function(title){ 
    return { 
     title: title 
    }; 
}; 

var folder = buildFolder(); 

folder.files.push(buildFile()); 
folder.files.push(buildFile()); 
folder.files.push(buildFile()); 
folder.files.push(buildFile()); 

folder.folders.push(buildFolder()); 
folder.folders[0].files.push(buildFile()); 
+0

對不起的人,你多在這個比我,你可以幫我翻譯這一點。謝謝 – Bungdaddy 2012-03-09 02:22:52

+0

明白了!感謝您向我解釋! – Bungdaddy 2012-03-09 02:27:03