2014-03-03 55 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

就是这样。非常感谢您的帮助。 –

相关问题