javascript / jquery – 从循环中以特定格式创建对象

我正在尝试将一些数据转换为这种格式,以便与名为mustache的模板系统一起使用:

{ "repo": [ { "name": "resque" }, { "name": "hub" }, { "name": "rip" }, ] } 

而我现在拥有的是:

 for (childIndex in scenes[sceneID].children) { childSceneID = scenes[sceneID].children[childIndex]; childScene = scenes[childSceneID]; } 

所以我不知何故需要让每个childScene成为“repo”对象中的“名称”。 有谁知道如何做到这一点? 这是胡子文档:

http://mustache.github.com/mustache.5.html

这是你的意思吗?:

 var repo = []; for (childIndex in scenes[sceneID].children) { childSceneID = scenes[sceneID].children[childIndex]; childScene = scenes[childSceneID]; repo.push({"name": childScene}); } var theobj = { "repo": repo }; 

我把它放在这里

 var repo = new Object(); var table = new Object(); repo["repo"] = table; table["name1"] = "resque"; table["name1"] = "hub"; table["name1"] = "rip";