I have this code. It's very good and has no error in it. But I don't like repeat the code html += "td"; a lot. Eaven html += "/td";. It is seing not clearly code.
How can I code it in shorter code using for loop?
html = "<table>";
html += "<tr>";
for(var name in newArticle[1]){
html += "<th>";
html += name;
html += "</th>";
}
html += "</tr>";
for (var i=0; i<newArticle.length; i++) {
html += "<tr>";
html += "<td>";
html += newArticle[i]["id"];
html += "</td>";
html += "<td>";
html += newArticle[i]["Forename"];
html += "</td>";
html += "<td>";
html += newArticle[i]["Surname"];
html += "</td>";
html += "<td>";
html += newArticle[i]["Age"];
html += "</td>";
html += "<td>";
html += newArticle[i]["City"];
html += "</td>";
html += "<td>";
html += newArticle[i]["ZIP"];
html += "</td>";
html += "</tr>";
}
html += "</table>";
console.log(html);
for(var name in newArticle[1]){
console.log(name);
}
You can note that newArticle() is an array and the table data is from array's elements.
newArticle()is not an array, it's a function call. But there's no function like that, it's undefined, which would make this whole thing to have at least one error. Your indentation is off, you don't properly declare variables and it's just bad, bad, bad. Welcome to StackOverflow.createElementand similar functions. Create element objects and append children to them.