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

위의 예는 두 제품에 대한 결과입니다. 나는이 데이터에 대한 액세스를 얻으려고하면 내가 기능 getSKU 상기 노드 선택에 다시 인수 s을 전달하고 때

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

getSKU(s238554); 


</script> 

나는 이것이 원인이되는 상상 문제를이 곳

이있다 데이터 객체 이 경우 콘솔 출력이 SKU의 제목이 될 것으로 예상됩니다 s238554. 나는 그러나 무엇을 얻을

은 다음과 같습니다 Uncaught ReferenceError: s238554 is not defined

내가 자바 스크립트 초보자입니다으로 제공 할 수있는 모든 안내를 부탁드립니다.

답변

0

액세스가 변수 아니다으로 따옴표 's238554' 내에서 속성 이름을 전달 SKUinfo.s.Title

SKUinfo[s].Title처럼 또한에 사용 []하여 속성입니다.

이와 비슷한 것.

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

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

정확히 일치합니다. 당신의 도움에 많은 감사드립니다. –