I have the following object:
var obj = {
key1: 'value1',
key2: 'value2',
key3: 'value3'
}
var obj = {
key1: 'value1',
key2: 'value2',
key3: 'value3'
}
$('#myTable thead tr').append('<th>Key Column</th>');
$('#myTable thead tr').append('<th>Value Column</th>');
var markup = '';
for (var i in obj) {
markup = "<tr><td>" + i + "</td></td><td>" + obj[i] + "</td></tr>";
$("#myTable tbody #first-col").append(markup);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr id="first-col">
</tr>
</tbody>
</table>
I want to create a table by looping the object. But I am getting like this:
+-------------+--------------+
| Key Column | Value Column |
+-------------+--------------+
| key1 value1 | |
+-------------+--------------+
| key2 value2 | |
+-------------+--------------+
| And so on.. | |
+-------------+--------------+
Kindly guide me how to perform it correctly. Or if it is better to generate the whole table again using jQuery, please let me know how to do it? Thanks