I am having trouble generating an HTML table from a json file using jQuery. The table has columns for Surgery_Date, Name, Insurance, Total, and CPT Payments. I want the last column to list the CPT codes and Payment in one cell with the intention to later make it a collapsable div or something once I get the basics working. My code generates the table ok except for the "Codes" cell fills with
[object Object],[object Object],[object Object],[object Object],[object Object]
I know I am fundamentally misunderstanding how the jQuery .each function returns values. I can run console.log(cptpmt) and see that it is iterating over the values correctly, I just cannot figure out how to get them into the html.
JSON example:
"326177": {
"Insurance": "Work comp",
"Name": "Doe, John",
"Payment_by_CPT": [
{
"CPT": "29823",
"Payment": "$170.38"
},
{
"CPT": "29824",
"Payment": "$183.56"
},
{
"CPT": "29826",
"Payment": "$200.18"
},
{
"CPT": "29827",
"Payment": "$1,167.98"
},
{
"CPT": "29828",
"Payment": "$504.47"
}
],
"Surgery_Date": "2017-06-29",
"Total": "$2,550.76"
},
HTML:
<table class="table table-bordered table-dark table-striped table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">Surgery Date</th>
<th scope="col">Name</th>
<th scope="col">Insurance</th>
<th scope="col">Total</th>
<th scope="col">Codes</th>
</tr>
</thead>
<tbody id="tableBody">
</tbody>
</table>
javascript:
$(function () {
$.each(data, function (index) {
let surg_date = data[index]['Surgery_Date']
let name = data[index]['Name']
let insurance = data[index]['Insurance']
let total = data[index]['Total']
let x = $.each(data[index]['Payment_by_CPT'], function (i) {
let cpt = data[index]['Payment_by_CPT'][i]['CPT']
let pmt = data[index]['Payment_by_CPT'][i]['Payment']
let cptpmt = cpt + ": " + pmt + "<br>"
return cptpmt
})
let html_str = "<tr>" +
"<td>" + surg_date + "</td>" +
"<td>" + name + "</td>" +
"<td>" + insurance + "</td>" +
"<td>" + total + "</td>" +
"<td>" + x + "</td>"
"</tr>"
$('#tableBody').append(html_str)