简体   繁体   English

传递函数参数以从对象检索数据

[英]Passing function argument to retrieve data from an object

I am have some trouble with a script I am working on. 我正在处理的脚本有一些麻烦。 I have been provided with an object with multiple items from a product catalog. 我从产品目录中获得了一个包含多个项目的对象。

What I am trying to do is to write a function which to which will allow me to render this data easily. 我试图做的是编写一个函数,使我可以轻松地呈现此数据。

<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>

The above example is the output I get for two products. 上面的示例是我获得两种产品的输出。

If I try to get access to this data this is where I have a problem 如果我尝试访问此数据,这就是我遇到的问题

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

getSKU(s238554);


</script>

I imagine this is being caused when I am passing the argument s back to the function getSKU a the node selection in the data object. 我想这是由于将参数s传递回getSKU函数和数据对象中的节点选择引起的。 In this I would expect the console output to be the Title from SKU s238554. 在这种情况下,我希望控制台输出为SKU s238554的标题。

What I get however, is: Uncaught ReferenceError: s238554 is not defined 但是,我得到的是: Uncaught ReferenceError: s238554 is not defined

I would appreciate any guidance that can be offered as I am a javascript novice. 我将不胜感激,因为我是javascript新手,可以提供任何指导。

Access your property by used [] on SKUinfo.s.Title like SKUinfo[s].Title 通过访问您的属性使用[]SKUinfo.s.TitleSKUinfo[s].Title

And also pass your property name within the quotes 's238554' as it's not variable. 并在属性名称中加上引号's238554'因为它不是变量。

Something like this. 这样的事情。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM