テーブルセルにデータを動的に取り込みしようとしています。私はこれに続いたJSfiddle example。ここでJavaScriptはデータをHTMLテーブルに動的に取り込みます
は私のコードです:
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}];
サンプル・データのようなあなたの配列を初期化する必要が定義されていない名前のキーのための単一引用符を閉じていなかったとして、無効なJSONですが、私は更新されていますJSON ...見てみましょう。 – Shiladitya