Click here to Skip to main content
15,561,685 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need the below output using a for loop as the output is a JSON object and I am not able to write for loop.

Any Suggestions would be a great help....


JavaScript
if (($('#tbl_Capacity tr').length - 1)==1)
    listcapac = [{ "roomId": 0, "capacity": $('#inputrowid' + 0).val() }]

if (($('#tbl_Capacity tr').length - 1) == 2)
    listcapac = [{ "roomId": 0, "capacity": $('#inputrowid' + 0).val() }, { "roomId": 1, "capacity": $('#inputrowid' + 1).val() }]

if (($('#tbl_Capacity tr').length - 1) == 3)
    listcapac = [{ "roomId": 0, "capacity": $('#inputrowid' + 0).val() }, { "roomId": 1, "capacity": $('#inputrowid' + 1).val() },
    { "roomId": 2, "capacity": $('#inputrowid' + 2).val() }]

if (($('#tbl_Capacity tr').length - 1) == 4)
    listcapac = [{ "roomId": 0, "capacity": $('#inputrowid' + 0).val() }, { "roomId": 1, "capacity": $('#inputrowid' + 1).val() },
    { "roomId": 2, "capacity": $('#inputrowid' + 2).val() }, { "roomId": 3, "capacity": $('#inputrowid' + 3).val() }]

if (($('#tbl_Capacity tr').length - 1) == 5)
    listcapac = [{ "roomId": 0, "capacity": $('#inputrowid' + 0).val() }, { "roomId": 1, "capacity": $('#inputrowid' + 1).val() },
    { "roomId": 2, "capacity": $('#inputrowid' + 2).val() }, { "roomId": 3, "capacity": $('#inputrowid' + 3).val() },
    { "roomId": 4, "capacity": $('#inputrowid' + 4).val() }]

if (($('#tbl_Capacity tr').length - 1) == 6)
    listcapac = [{ "roomId": 0, "capacity": $('#inputrowid' + 0).val() }, { "roomId": 1, "capacity": $('#inputrowid' + 1).val() },
    { "roomId": 2, "capacity": $('#inputrowid' + 2).val() }, { "roomId": 3, "capacity": $('#inputrowid' + 3).val() },
    { "roomId": 4, "capacity": $('#inputrowid' + 3).val() }, { "roomId": 5, "capacity": $('#inputrowid' + 5).val() }]

if (($('#tbl_Capacity tr').length - 1) == 7)
    listcapac = [{ "roomId": 0, "capacity": $('#inputrowid' + 0).val() }, { "roomId": 1, "capacity": $('#inputrowid' + 1).val() },
    { "roomId": 2, "capacity": $('#inputrowid' + 2).val() }, { "roomId": 3, "capacity": $('#inputrowid' + 3).val() },
    { "roomId": 4, "capacity": $('#inputrowid' + 4).val() }, { "roomId": 5, "capacity": $('#inputrowid' + 5).val() },
    { "roomId": 6, "capacity": $('#inputrowid' + 6).val() }]

if (($('#tbl_Capacity tr').length - 1) == 8)
    listcapac = [{ "roomId": 0, "capacity": $('#inputrowid' + 0).val() }, { "roomId": 1, "capacity": $('#inputrowid' + 1).val() },
    { "roomId": 2, "capacity": $('#inputrowid' + 2).val() }, { "roomId": 3, "capacity": $('#inputrowid' + 3).val() },
    { "roomId": 4, "capacity": $('#inputrowid' + 3).val() }, { "roomId": 5, "capacity": $('#inputrowid' + 5).val() },
    { "roomId": 6, "capacity": $('#inputrowid' + 6).val() }, { "roomId": 7, "capacity": $('#inputrowid' + 7).val() }]


What I have tried:

I have tried to serialize the data using all the techniques but after applying forloop my var object is not stringyfying to JSON Format.. Requesting help...
Posted
Updated 7-May-16 7:17am
v4

Converting this to a for loop seems simple enough:
JavaScript
listcapac = [];
var rooms = $('#tbl_Capacity tr').length - 1;
for (var i = 0; i < rooms; i++) {
    listcapac.push({
        "roomId": i,
        "capacity": $('#inputrowid' + i).val()
    });
}
 
Share this answer
 
Comments
Orugallu Technologies 7-May-16 13:12pm    
Thank you so much Richard Deeming.. it worked like a charm..
JSON serialization is already available. See, for example,
JSON.stringify() — JavaScript | MDN[^],
JSON.parse() — JavaScript | MDN[^].

See also: JSON — JavaScript | MDN[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900