Hi everyone,
I recently started learning D3.js and would like to create an html table returning the columns & values of a json file (1000 rows, 7 columns). The table return data in html table except the last two column names which are not displaying although the values do.
Here is my attempt:
<body>
<section class="container">
<h2>Data exploration</h2>
<table id="table_data" cellspacing="2" cellpadding="2">
</table>
</section>
</body>
<!-- Create table with D3.js -->
<script type="text/javascript">
d3.json("/files/ny_cause_death.json").then(ShowData)
function ShowData(data) {
const table = d3.select("#table_data")
const thead = table.append("thead")
.append("tr")
.selectAll("th")
.data( d3.keys(data[0]) ) // Without [0] || d3.map(data).keys() retuns rows index e.g 1.2.3.4 etc
const tbody = table.append("tbody").selectAll("td").data(data.slice(1)).enter().append("tr")
thead.enter().append("th").text(function(k) { return k} )
tbody.append("td").text(function(v) { return v['year']})
tbody.append("td").text(function(k) { return k["leading_cause"] })
tbody.append("td").text(function(k) { return k["sex"] })
tbody.append("td").text(function(k) { return k["race_ethnicity"] })
tbody.append("td").text(function(k) { return k["deaths"] })
tbody.append("td").text(function(k) { return k["death_rate"] })
tbody.append("td").text(function(k) { return k["age_adjusted_death_rate"] })
}
</script>
Link to the dataset I am using: https://data.cityofnewyork.us/resource/jb7j-dtam.json
And here is a screenshot of missing columns:
Can someone tell me what is the root cause for this?
Thanks in advance.
Kind regards,
Kanatbek