Here is a JSFiddle that shows how to print the data in your object:
http://jsfiddle.net/4PVr5/1/
And the code:
HTML
<table id="table">
<tr>
</tr>
</table>
JAVASCRIPT
var object = {
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014"
};
for (var prop in object) {
// important check that this is objects own property
// not from prototype prop inherited
if(object.hasOwnProperty(prop)){
var td = document.createElement("td");
var strong = document.createElement("strong");
var text = document.createTextNode(prop + " - " + object[prop]);
strong.appendChild(text);
td.appendChild(strong);
document.getElementById("table").appendChild(td);
}
}
EDIT UPDATE TO angus_thermopylae:
I have updated the JSFiddle to show the concept: http://jsfiddle.net/4PVr5/12/
Then you can have as many properties on the object you want but only print the ones you defined and in the order you defined. You just add a text string and then you got another print.
EDIT UPDATE:
I updated the code to follow the table headers. Now it adds them directly and also handles objects with too few properties.
HTML
<table id="table">
<thead>
<th id="publicationDate"></th>
<th id="contracted"></th>
<th id="contracting"></th>
<th id="id"></th>
<th id="objectBriefDescription"></th>
<th id="initialContractualPrice"></th>
<th id="signingDate"></th>
</thead>
<tbody>
</tbody>
</table>
JAVASCRIPT
var orderedObject = {
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014"
};
var unorderedObject = {
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014",
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
};
var toManyPropertiesObject = {
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014",
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
newProp: "ignored",
newProp1: "ignored",
newProp2: "ignored",
};
var toFewPropertiesObject = {
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
};
printObjectInTable(orderedObject, "table");
printObjectInTable(unorderedObject, "table");
printObjectInTable(toManyPropertiesObject, "table");
printObjectInTable(toFewPropertiesObject, "table");
function printObjectInTable(objectToIterate, tableID) {
var thChildren = document.getElementById(tableID).getElementsByTagName("th"),
childrenLength = thChildren.length,
tr = document.createElement("tr");
for (var i = 0; i < thChildren.length; i++) {
var th = thChildren[i];
// important check that this is objects own property
// not from prototype prop inherited
var td = document.createElement("td");
if (objectToIterate.hasOwnProperty(th.id)) {
td.appendChild(document.createTextNode(objectToIterate[th.id]));
}
tr.appendChild(td);
}
document.getElementById(tableID).getElementsByTagName("tbody")[0].appendChild(tr);
}
"28-02-2014","Servicash - Equipamentos Electrónicos, Lda.","Banco de Portugal", please make me correct if i am wrong.