2014-03-03 53 views
0

我在處理腳本時遇到了一些問題。我從一個產品目錄中提供了一個包含多個項目的對象。傳遞函數參數以從對象中檢索數據

我想要做的是編寫一個函數,它可以讓我輕鬆渲染這些數據。

<script type="application/javascript"> 
SKUinfo = 
{ 
    "s238554": { 
    "Age": { 
     "Description": "Age 18+", 
     "Thumbnail": "/productImages/assets/img/icon18.gif" 
    }, 
    "Barcode": { 
     "Barcode": "50622132430794" 
    }, 
    "Currency": "£", 
    "Description": "Description goes here", 
    "Id": 44305, 
    "Packshots": [ 
     "/productImages/238556/1min.jpg", 
     "/productImages/238556/2med.jpg", 
     "/productImages/238556/3max.jpg" 
    ], 
    "Pegis": [], 
    "Platform": { 
     "Button": "Xbox 360", 
     "ID": 0 
    }, 
    "Publisher": { 
    "Description": null 
    }, 
    "Release": "/Date(1392940800000+0000)/", 
    "Screenshots": [ 
     { 
     "ScreenshotMax": "/productImages/238556/5scrmax1.jpg", 
     "ScreenshotMin": "/productImages/238556/4scrmin1.jpg" 
     } 
    ], 
    "Title": "Product title 2 goes here", 
    "Variants": [ 
     { 
     "Id": 58242, 
     "MaxOrderQuantity": 3, 
     "Presellable": true, 
     "Price": 29.97, 
     "PriceCultureFormat": "29.97", 
     "PriceWithCurrencyFormat": "£29.97", 
     "Sku": 238556, 
     "Type": { 
      "Description": "New" 
     } 
     }, 
    ], 
    "Vendor": { 
     "Description": "" 
    }, 
    }, 
    "s238556": { 
    "Age": { 
     "Description": "Age 18+", 
     "Thumbnail": "/productImages/assets/img/pegi/icon18.gif" 
    }, 
    "Barcode": { 
     "Barcode": "5060134530794" 
    }, 
    "Currency": "£", 
    "Description": "Description here", 
    "Id": 654654, 
    "Packshots": [ 
     "/productImages/238556/1min.jpg", 
     "/productImages/238556/2med.jpg", 
     "/productImages/238556/3max.jpg" 
    ], 
    "Pegis": [], 
    "Platform": { 
     "Button": "PlayStation 3", 
     "ID": 0 
    }, 
    "Publisher": { 
     "Description": null 
    }, 
    "Release": "/Date(1392940800000+0000)/", 
    "Screenshots": [ 
     { 
     "ScreenshotMax": "/productImages/238556/5scrmax1.jpg", 
     "ScreenshotMin": "/productImages/238556/4scrmin1.jpg" 
     }, 
     { 
     "ScreenshotMax": "/productImages/238556/7scrmax2.jpg", 
     "ScreenshotMin": "/productImages/238556/6scrmin2.jpg" 
     }, 
    ], 
    "Title": "Product title 2 goes here", 
    "Variants": [ 
     { 
     "Id": 58242, 
     "MaxOrderQuantity": 3, 
     "Presellable": true, 
     "Price": 29.97, 
     "PriceCultureFormat": "29.97", 
     "PriceWithCurrencyFormat": "£29.97", 
     "Sku": 238556, 
     "Type": { 
      "Description": "New" 
     } 
     }, 
    ], 
    "Vendor": { 
     "Description": "" 
    }, 
    "VideoHTML": "html here", 
    "status": { 
     "Response": "product found", 
     "Success": true 
    } 
    } 
} 
</script> 

上面的例子是輸出我得到了兩個產品。

如果我試圖讓這個數據的訪問,這是在那裏我有一個問題

<script type="application/javascript"> 
function getSKU(s) 
{ 
     console.log(SKUinfo.s.Title); 
} 

getSKU(s238554); 


</script> 

我想這正引起,當我路過的說法s回功能getSKU一個節點選擇在數據對象。在這我希望控制檯輸出是來自SKU s238554的標題。

但是我所得到的是:Uncaught ReferenceError: s238554 is not defined

我希望能爲我一個javascript新手提供的任何指導。

回答

0

訪問您在SKUinfo.s.TitleSKUinfo[s].Title

而且也引號's238554'內通過您的屬性名稱,因爲它不是變量財產使用[]

就是這樣。

function getSKU(s){ 
    console.log(SKUinfo[s].Title); 
} 

getSKU('s238554'); // s238554 within quotes. 
+0

就是這樣。非常感謝您的幫助。 –

相關問題