我需要将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 :(得分:0)
您可以使用对象
function ThemesCollection(Nodeid, parentid, theme, Document) {
this.Nodeid = Nodeid;
this.parentid = parentid;
this.theme = theme;
this.Document = Document;
}
然后使用ThemesCollection
个对象的数组(如果你用ajax检索它)
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'])
}
}
答案 1 :(得分: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,“Theme X”,“Document foobar”])
答案 2 :(得分:0)
使用JavaScript Object Notation。
var info = [
{
"Nodeid" : 4787814,
"parented" : 4787484,
"theme" : "Theme1",
"Document" : "Document 1"
},
{
"Nodeid" : 4787824,
"parented" : 4787484,
"theme" : "Theme2",
"Document" : "NULL"
}]
然后,您可以像这样使用二维数组;
alert(info[0].theme)