My array looks like this
productTable:Array(3)
'[{
id:1
name:"Apple"
today:10
yearly:21
},
{
id:2
name:"Banana"
today:14
yearly:45
},
{
id:2
name:"Mangoes"
today:5
yearly:95
},
I want to make table like this
Period | Apple | Banana | Mangoes |
-------+--------+---------+---------+
Yearly | 21 | 45 | 95 |
Today | 10 | 14 | 5 |
and my html code is where id=product title is for Product title & id=product_item is for data like above.
<table>
<tr id="product_title">
</tr>
<tbody>
<tr id="product_item">
</tr>
</tbody>
</table>
My script file is this; I am getting an error from this. All data are getting undefined.
<script>
fetch('http://127.0.0.1:8000/api/productTable')
.then(response=>response.json())
.then(data=>{
$('#product_title').append("<th>Period</th>");
data.productTable.forEach(function (product) {
$('#product_title').append('<th>'+product.name +'</th>');
});
$.each(['yearly','today'],function (i,period) {
$('#product_item').append('<td>'+ period +'</td>');
$.each(data.retailsTable, function(result) {
$('#product_item').append('<td>'+ result.period +'</td>');
});
})
});
</script>