Instead of printing out the employees info in a line, I would like to use the variable "obj" to create a html table wif all the employees info. However, I am unable to pass in "obj". Please advise.
var text = '{"employees":[' + '{"Name":"Tony","Mobile":"99221111","Email":"tony@json.com" },' + '{"Name":"Linda","Mobile":"98981111","Email":"linda@json.com" },' + '{"Name":"Patrick","Email":"patrick@json.com" },' + '{"Name":"Isabella","Mobile":"99552222" }]}';
obj = JSON.parse(text);
function buildHtmlTable(selector) {
var columns = addAllColumnHeaders(obj, selector);
for (var i = 0; i < obj.length; i++) {
var row$ = $('<tr/>');
for (var colIndex = 0; colIndex < columns.length; colIndex++) {
var cellValue = obj[i][columns[colIndex]];
if (cellValue == null) cellValue = "";
row$.append($('<td/>').html(cellValue));
}
$(selector).append(row$);
}
}
// Adds a header row to the table and returns the set of columns.
// Need to do union of keys from all records as some records may not contain
// all records.
function addAllColumnHeaders(obj, selector) {
var columnSet = [];
var headerTr$ = $('<tr/>');
for (var i = 0; i < obj.length; i++) {
var rowHash = obj[i];
for (var key in rowHash) {
if ($.inArray(key, columnSet) == -1) {
columnSet.push(key);
headerTr$.append($('<th/>').html(key));
}
}
}
$(selector).append(headerTr$);
<!DOCTYPE html>
<html>
<head>
<title>Task 1</title>
</head>
<body onLoad="buildHtmlTable('#excelDataTable')">
<table id="excelDataTable" border="1">
</table>
<p id="demo"></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "Task1ab.js"></script>
</body>
</html>