Exercise 13.01
Build a Table
function buildTable(data) {
var table = document.createElement('table');
var tr = document.createElement('tr');
var td = document.createElement('td');
var keyNames = Object.keys(data[0]);
for(var i = 0; i < keyNames.length; i++){
var th = document.createElement('th');
var title = document.createTextNode(keyNames[i]);
th.appendChild(title);
tr.appendChild(th);
}
table.appendChild(tr);
data.forEach(function(object){
var row = document.createElement("tr");
keyNames.forEach(function(keyName){
var cell = document.createElement("td");
cell.textContent = object[keyName];
row.appendChild(cell);
});
table.appendChild(row);
});
return table;
}
document.body.appendChild(buildTable(MOUNTAINS));
To open the JavaScript console, press F12 or on MAC press COMMAND-OPTION-I.