2 dimensional array in javascript
i would need to store in a javascript variable the following table:
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
Is there any jquery or javascript code that could help me storing this table in a similar structure. By the moment i have the following javascript code.
var ThemesCollection=
{
Themes: {},
开发者_StackOverflow社区 Initialize: function()
{
ThemesCollection.Themes=new Object();
}
}
You can use objects
function ThemesCollection(Nodeid, parentid, theme, Document) {
this.Nodeid = Nodeid;
this.parentid = parentid;
this.theme = theme;
this.Document = Document;
}
and then use array of ThemesCollection
objects (if you retreive it with 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'])
}
}
What about a matrix (array of arrays)?
[ [ 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" ] ]
If x
is your matrix you just add new items by doing:
x.push([4787859, 4787824, "Theme X", "Document foobar"])
Use JavaScript Object Notation.
var info = [
{
"Nodeid" : 4787814,
"parented" : 4787484,
"theme" : "Theme1",
"Document" : "Document 1"
},
{
"Nodeid" : 4787824,
"parented" : 4787484,
"theme" : "Theme2",
"Document" : "NULL"
}]
Then, you can use your two dimensional array like this;
alert(info[0].theme)
精彩评论