【发布时间】:2018-01-11 20:10:23
【问题描述】:
我正在尝试用数据动态填充表格单元格。我关注了这个JSfiddle example。
这是我的代码:
var heading = new Array();
heading[0] = "Type"
heading[1] = "Item Name"
heading[2] = "Brand"
heading[3] = "Unit Price"
heading[4] = "Quantity"
var stock = new Array();
for(var i = 0; i < topProductList.length; i++){
stock[0].push(topProductList[i].type);
stock[1].push(topProductList[i].itemName);
stock[2].push(topProductList[i].brand);
stock[3].push(topProductList[i].unitprice);
stock[4].push(topProductList[i].quantity);
console.log(topProductList[i].type + ' ' + topProductList[i].itemName + ' ' + topProductList[i].brand + ' ' + topProductList[i].unitprice + ' ' + topProductList[i].quantity);
}
我尝试通过循环遍历我的数组来为每一列添加数据。但是,我收到了Cannot read property 'push' of undefined。我打印出console.log 来检查我是否正确检索并且数据检索没有问题。只是我尝试将每个数据添加到列中的部分遇到了问题。
有什么想法吗?
topProductList 的示例数据:
var topProductList = [{type: 'home appliance', name: 'Sound Teoh Tv Cable 5m Hl3599 Soundteoh', unitprice: 9.90, quantity: 5},
{type: 'kitchen appliance', name: ' Powerpac Mini Rice Cooker Pprc09 Powerpac', unitprice: 19.90, quantity: 5},
{type: 'kitchen appliance', name: ' Sona 2-slice Bread Toaster Sto2201 Sona', unitprice: 39.90, quantity: 5},
{type: 'home appliance', name: ' Simply Living. 10" Wall Clock Simply Living', unitprice: 9.90, quantity: 5},
{type: 'kitchen appliance', name: ' Morries Pie Maker Ms-8028pm Morries', unitprice: 29.90, quantity: 4}];
【问题讨论】:
-
topProductList 的示例数据是无效的 JSON,因为您没有关闭 name 键的单引号,我已经更新了 JSON ...看看。
标签: javascript html arrays html-table