Sunday, 1 October 2017

Dynamically build JSON object list from Javascript

I'm trying to end up with the below JSON from an HTML form.



{
"Name":"Curtis",
"Phone":"555-555-5555",
"Replacements":
[
{
"Company":"ABC Company",

"Amount":100
},
{
"Company":"123 Company",
"Amount":200
},
]
}



I'm struggling with the JavaScript in regards to building the array for the replacements.



var o = {};
o["Name"] = $("#Name").val();
o["Phone"] = $("#Phone").val();

//How do I append the dynamic list of replacements here?
//$("#Company1").val();
//$("#Amount1").val();
//$("#Company2").val();

//$("#Amount2").val();

$("#txtJSON").val(JSON.stringify(o));

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...