Larry Maner Larry Maner - 3 years ago 107
jQuery Question

Trying to form Object Array in jQuery

I'm trying to structure an object to follow the exact format of a firebase DB. The firebase DB looks like this:

{
"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>"
}
}

}


I want an Object array that looks just like that. I've tried the following code but I'm getting an undefined when trying to push to the top key.

databaseArray.push("allUserForms");

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


Waiver, name and verbiage are all values I set earlier in the function from the DB. I know it's something easy that I'm not getting.

Answer Source

Here is what I would do with an object rather than an array:

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 };
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download