试图在jQuery中形成Object Array

时间:2017-09-30 19:07:23

标签: jquery object firebase

我试图构建一个对象,以遵循firebase数据库的确切格式。 firebase DB看起来像这样:

{
  "allUserForms":{
      "waiver-form-1" : {
        "name" : "Waiver Form 1",
        "verbiage" : "<p>Waiver Form 1</p>"
      },
      "waiver-form-2" : {
        "name" : "Waiver Form 2",
        "verbiage" : "<p>Waiver Form 2</p>"
      },
      "waiver-form-3" : {
        "name" : "Waiver Form 3",
        "verbiage" : "<p>Waiver Form 3</p>"
      },
      "waiver-form-4" : {
        "name" : "Waiver Form 4",
        "verbiage" : "<p>Waiver Form 4</p>"
      },
      "waiver-form-5" : {
        "name" : "Waiver Form 5",
        "verbiage" : "<p>Waiver Form 5</p>"
      }
  }

}

我想要一个看起来就像那样的Object数组。我尝试过以下代码,但在尝试按顶键时我得到了一个未定义的代码。

databaseArray.push("allUserForms");

databaseArray.allUserForms.push({ "waiver": waiver, "name": name, "verbiage": verbiage });

豁免,名称和措词都是我之前在数据库中设置的值。我知道这很容易,我没有得到。

1 个答案:

答案 0 :(得分:0)

这是我用对象而不是数组做的事情:

var databaseData = {}; // an object
databaseData.allUserForms = {}; // to match your example

for(var i = 0; i > 10; i++) {
    databaseData.allUserForms['waiver-form-'+i] = { "waiver": waiver, "name": name, "verbiage": verbiage };
}