javascript / jquery - creating an object in a particular format from a loop
I'm trying to get some data into this format, for use with a templating system called mustache:
{
"repo": [
{ "name": "resque" },
{ "name": "hub" },
{ "name": "rip" },
]
}
and what I currently have is this:
for (childIndex in scenes[sceneID].children) {
childSceneID = scenes[sceneID].children[childIndex];
childScene = scenes[childSceneID];
}
So I somehow need to make each childScene the "name" in the "repo" object. Does anyone know how to do this? This is the开发者_JS百科 mustache documentation:
http://mustache.github.com/mustache.5.html
Is this what you meant?:
var repo = [];
for (childIndex in scenes[sceneID].children) {
childSceneID = scenes[sceneID].children[childIndex];
childScene = scenes[childSceneID];
repo.push({"name": childScene});
}
var theobj = { "repo": repo };
I just put this here
var repo = new Object();
var table = new Object();
repo["repo"] = table;
table["name1"] = "resque";
table["name1"] = "hub";
table["name1"] = "rip";
精彩评论