I'm trying to build a HTML table with data from a JSON structured array such as:
var myjson = [
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName": "Jones"}
];
I know I can use:
var tbl = "<table>"
$.each(myjson, function(x, y) {
tbl += "<tr>";
tbl += "<td>firstName</td>";
tbl += "<td>lastName</td>";
tbl += "</tr>";
tbl += "<tr>";
tbl += "<td>" + y.firstName + "</td>";
tbl += "<td>" + y.lastName + "</td>";
tbl += "</tr>";
});
tbl += "</table>";
but how can I get the same result if I don't know what the column names (firstName, lastName) are called? How can I iterate through the key/values of each object in the JSON structured array?
In each array, the number of elements in each object will be the same