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

时间:2014-03-03 15:58:51

标签: javascript json function object arguments

我正在处理我正在编写的脚本有些麻烦。我收到了一个包含产品目录中多个项目的对象。

我要做的是编写一个函数,以便我可以轻松地呈现这些数据。

<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新手。

1 个答案:

答案 0 :(得分:0)

使用[] SKUinfo.s.Title SKUinfo[s].Title上使用's238554'访问您的媒体资源,例如function getSKU(s){ console.log(SKUinfo[s].Title); } getSKU('s238554'); // s238554 within quotes.

并且还会在引号{{1}}内传递您的媒体资源名称,因为它不是变量。

像这样。

{{1}}