2010-09-15 75 views
0

我需要一个JavaScript变量下表中存储:2维数组在javascript

Nodeid parentid theme   Document 

4787814 4787484 Theme1  Document 1 
4787824 4787484 Theme2  NULL 
4787834 4787824 Theme2.1  Document 1 of theme 2.1 
4787834 4787824 Theme2.1  Document 2 of theme 2.1 
4787834 4787824 Theme2.1  Document 3 of theme 2.1 
4787844 4787824 Theme2.2  Document 1 of theme 2.2 
4787854 4787824 Theme2.2  Document 2 of theme 2.2 

是否有任何的jQuery或JavaScript代码,可以帮助我存储在类似的结构此表。到目前为止,我有以下JavaScript代码。

var ThemesCollection= 
    { 
     Themes: {}, 
     Initialize: function() 
     { 
      ThemesCollection.Themes=new Object(); 
     } 
    } 

回答

0

您可以使用对象

function ThemesCollection(Nodeid, parentid, theme, Document) { 
    this.Nodeid = Nodeid; 
    this.parentid = parentid; 
    this.theme = theme; 
    this.Document = Document; 
} 

然后用ThemesCollection对象的数组(如果你使用Ajax retreive吧)

function on_success(data) { 
    for (row in data) { 
    globalThemes.push(new ThemesCollection(row[0], row[1], row[2], row[3]); 
    // or i row is object 
    //new ThemesCollection(row['Nodeid'], row['parentid'], row['theme'], row['Document']) 
    } 
} 
+0

太棒了!感谢了很多。 – Jose3d 2010-09-15 08:46:47

0

什么矩阵(数组的数组)?

[ [ 4787814, 4787484, "Theme1",  "Document 1" ], 
    [ 4787824, 4787484, "Theme2",  null ], 
    [ 4787834, 4787824, "Theme2.1",  "Document 1 of theme 2.1" ], 
    [ 4787834, 4787824, "Theme2.1",  "Document 2 of theme 2.1" ], 
    [ 4787834, 4787824, "Theme2.1",  "Document 3 of theme 2.1" ], 
    [ 4787844, 4787824, "Theme2.2",  "Document 1 of theme 2.2" ], 
    [ 4787854, 4787824, "Theme2.2",  "Document 2 of theme 2.2" ] ] 

如果x是你的矩阵,你只是做添加新的项目:

x.push([4787859,4787824, “主题X”, “文档foobar的”])

0

使用JavaScript对象表示法。

var info = [ 
{ 
"Nodeid" : 4787814, 
"parented" : 4787484, 
"theme" : "Theme1", 
"Document" : "Document 1" 
}, 
{ 
"Nodeid" : 4787824, 
"parented" : 4787484, 
"theme" : "Theme2", 
"Document" : "NULL" 
}] 

然后,你可以使用你的二维数组像这样;

alert(info[0].theme)