javascript / jquery - 从循环中创建特定格式的对象 [英] javascript / jquery - creating an object in a particular format from a loop

查看:92
本文介绍了javascript / jquery - 从循环中创建特定格式的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将这些格式的数据用于名为mustache的模板系统:

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" },
  ]
}

我目前拥有的是:

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

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

So I somehow need to make each childScene the "name" in the "repo" object. Does anyone know how to do this? This is the mustache documentation:

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 };

这篇关于javascript / jquery - 从循环中创建特定格式的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆