Baran Topal

Baran Topal


May 2024
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories


Populate a table where each component of JSON corresponds each table row

baranbaran

Long ago, I wanted to populate a table where each component of JSON corresponds each table row and here is the solution:

var json = {
 "component0": {
 "top": "99",
 "bottom": "14",
 "surcharge": "11"
 },
 "component3": {
 "top": "99",
 "bottom": "14",
 "surcharge": "11"
 },
 "component4": {
 "top": "99",
 "bottom": "14",
 "surcharge": "11"
 },
 "component1": {
 "top": "99",
 "bottom": "14",
 "surcharge": "11"
 },
 "component2": {
 "top": "99",
 "bottom": "14",
 "surcharge": "11"
 },
 "component7": {
 "top": "99",
 "bottom": "14",
 "surcharge": "11"
 },
 "component5": {
 "top": "99",
 "bottom": "14",
 "surcharge": "11"
 },
 "component6": {
 "top": "99",
 "bottom": "14",
 "surcharge": "11"
 }
}

$(document).ready(function() {
 for(j in json) {
 var t = json[j].top;
 var b = json[j].bottom;
 var s = json[j].surcharge;
 $("#mytable").append("<tr><td>" + t + "</td><td>" + b + "</td><td>" + s + "</td></tr>");
 }
})