Javascript 传递函数参数以从对象检索数据
我在写剧本时遇到了一些麻烦。我收到了一个包含产品目录中多个项目的对象 我试图做的是编写一个函数,它允许我轻松地呈现这些数据Javascript 传递函数参数以从对象检索数据,javascript,json,function,object,arguments,Javascript,Json,Function,Object,Arguments,我在写剧本时遇到了一些麻烦。我收到了一个包含产品目录中多个项目的对象 我试图做的是编写一个函数,它允许我轻松地呈现这些数据 <script type="application/javascript"> SKUinfo = { "s238554": { "Age": { "Description": "Age 18+", "Thumbnail": "/productImages/assets/img/icon18.gif" }, "B
<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>
SKUinfo=
{
“s238554”:{
“年龄”:{
“描述”:“18岁以上”,
“缩略图”:“/productImages/assets/img/icon18.gif”
},
“条形码”:{
“条形码”:“50622132430794”
},
“货币”:“英镑”,
“说明”:“说明在这里”,
“Id”:44305,
“打包照片”:[
“/productImages/238556/1min.jpg”,
“/productImages/238556/2med.jpg”,
“/productImages/238556/3max.jpg”
],
“Pegis”:[],
“平台”:{
“按钮”:“Xbox 360”,
“ID”:0
},
“出版商”:{
“说明”:空
},
“发布”:“/日期(139294080000+0000)/”,
“截图”:[
{
“ScreenshotMax”:“/productImages/238556/5scrmax1.jpg”,
“ScreenshotMin”:“/productImages/238556/4scrmin1.jpg”
}
],
“标题”:“产品标题2在此显示”,
“变体”:[
{
“Id”:58242,
“MaxOrderQuantity”:3,
“可预售”:正确,
“价格”:29.97,
“价格文化格式”:“29.97”,
“PriceWithCurrencyFormat”:“29.97英镑”,
“Sku”:238556,
“类型”:{
“说明”:“新”
}
},
],
“供应商”:{
“说明”:”
},
},
“s238556”:{
“年龄”:{
“描述”:“18岁以上”,
“缩略图”:“/productImages/assets/img/pegi/icon18.gif”
},
“条形码”:{
“条形码”:“5060134530794”
},
“货币”:“英镑”,
“说明”:“此处说明”,
“Id”:654654,
“打包照片”:[
“/productImages/238556/1min.jpg”,
“/productImages/238556/2med.jpg”,
“/productImages/238556/3max.jpg”
],
“Pegis”:[],
“平台”:{
“按钮”:“PlayStation 3”,
“ID”:0
},
“出版商”:{
“说明”:空
},
“发布”:“/日期(139294080000+0000)/”,
“截图”:[
{
“ScreenshotMax”:“/productImages/238556/5scrmax1.jpg”,
“ScreenshotMin”:“/productImages/238556/4scrmin1.jpg”
},
{
“ScreenshotMax”:“/productImages/238556/7scrmax2.jpg”,
“ScreenshotMin”:“/productImages/238556/6scrmin2.jpg”
},
],
“标题”:“产品标题2在此显示”,
“变体”:[
{
“Id”:58242,
“MaxOrderQuantity”:3,
“可预售”:正确,
“价格”:29.97,
“价格文化格式”:“29.97”,
“PriceWithCurrencyFormat”:“29.97英镑”,
“Sku”:238556,
“类型”:{
“说明”:“新”
}
},
],
“供应商”:{
“说明”:”
},
“VideoHTML”:“此处为html”,
“地位”:{
“响应”:“找到产品”,
“成功”:真的吗
}
}
}
上面的例子是我得到的两个产品的输出
如果我试图访问这些数据,这就是我遇到的问题
<script type="application/javascript">
function getSKU(s)
{
console.log(SKUinfo.s.Title);
}
getSKU(s238554);
</script>
函数getSKU(s)
{
控制台日志(SKUinfo.s.Title);
}
getSKU(s238554);
我想象这是在我将参数s
传递回函数getSKU
a时导致的,该函数是数据对象中的节点选择。在本文中,我希望控制台输出是来自SKU s238554的标题
然而,我得到的是:uncaughtreferenceerror:s238554没有定义
由于我是一名javascript新手,我将非常感谢您提供的任何指导。通过使用
SKUinfo.s.Title
上的[]
访问您的财产,如SKUinfo[s]。Title
并在引号中传递属性名,因为它不是变量
像这样的
function getSKU(s){
console.log(SKUinfo[s].Title);
}
getSKU('s238554'); // s238554 within quotes.