Lets have a wider approach. Assuming that you have to create a table with some data in it. That data has to be provided in an object structure in a similar fashion to the following;
[ { Prop_0: 'Value_0',
Prop_1: 'Value_1',
Prop_2: 'Value_2',
Prop_3: 'Value_3',
Prop_4: 'Value_4' },
{ Prop_0: 'Value_5',
Prop_1: 'Value_6',
Prop_2: 'Value_7',
Prop_3: 'Value_8',
Prop_4: 'Value_9' },
{ Prop_0: 'Value_10',
Prop_1: 'Value_11',
Prop_2: 'Value_12',
Prop_3: 'Value_13',
Prop_4: 'Value_14' },
{ Prop_0: 'Value_15',
Prop_1: 'Value_16',
Prop_2: 'Value_17',
Prop_3: 'Value_18',
Prop_4: 'Value_19' },
{ Prop_0: 'Value_20',
Prop_1: 'Value_21',
Prop_2: 'Value_22',
Prop_3: 'Value_23',
Prop_4: 'Value_24' } ]
In which the properties will designate the headers and the values are the <td> cell values. tableMaker will take this object as first argument and turn it into a table HTML mark up text. If the second argument is true then the first object's properties will be used for <th> data. So lets see...
function tableMaker(o,h) {
var keys = o.length && Object.keys(o[0]),
rowMaker = (a,t) => a.reduce((p,c,i,a) => p + (i === a.length-1 ? "<" + t + ">" + c + "</" + t + "></tr>"
: "<" + t + ">" + c + "</" + t + ">"),"<tr>"),
rows = o.reduce((r,c) => r + rowMaker(keys.reduce((v,k) => v.concat(c[k]),[]),"td"),h ? rowMaker(keys,"th") : []);
return rows.length ? "<table>" + rows + "</table>" : "";
}
var data = [{"Prop_0":"Value_0","Prop_1":"Value_1","Prop_2":"Value_2","Prop_3":"Value_3","Prop_4":"Value_4"},{"Prop_0":"Value_5","Prop_1":"Value_6","Prop_2":"Value_7","Prop_3":"Value_8","Prop_4":"Value_9"},{"Prop_0":"Value_10","Prop_1":"Value_11","Prop_2":"Value_12","Prop_3":"Value_13","Prop_4":"Value_14"},{"Prop_0":"Value_15","Prop_1":"Value_16","Prop_2":"Value_17","Prop_3":"Value_18","Prop_4":"Value_19"},{"Prop_0":"Value_20","Prop_1":"Value_21","Prop_2":"Value_22","Prop_3":"Value_23","Prop_4":"Value_24"}],
tableHTMLText = tableMaker(data,true);
document.write(tableHTMLText);
console.log(tableHTMLText);
I hope it helps.