簡體   English   中英

如何訪問復雜結構的數據?

[英]How to access data in complex structure?

我正在嘗試整理網站的心願單功能,為此,我需要從另一個數組中的一個數組中調用一個值,我需要獲取底層products對象的值,具體取決於所需的第4級options對象(取決於在第4級ID(在這種情況下為44、9、8、7、6、475)):

var jsonProductData = {
    "attributes" : {
        "133": {
            "id":"133",
            "code":"size",
            "label":"Size",
            "options": [
                {"id":"44","label":"XS","price":"0","oldPrice":"0","cssclass":"","products":["11921"]},
                {"id":"9","label":"S","price":"0","oldPrice":"0","cssclass":"","products":["11922"]},
                {"id":"8","label":"M","price":"0","oldPrice":"0","cssclass":"","products":["11923"]},
                {"id":"7","label":"L","price":"0","oldPrice":"0","cssclass":"","products":["11924"]},
                {"id":"6","label":"XL","price":"0","oldPrice":"0","cssclass":"","products":["11925"]},
                {"id":"475","label":"XXL","price":"0","oldPrice":"0","cssclass":"","products":["11926"]}
            ]
        }
    },
    "template" : "\u00a3#{price}",
    "basePrice" : "187",
    "oldPrice" : "299.99",
    "productId" : "11950",
    "chooseText" : "Select Size...",
    "taxConfig" : {
        "includeTax" : false,
        "showIncludeTax" : true,
        "showBothPrices" : false,
        "defaultTax" : 0,
        "currentTax" : 0,
        "inclTaxTitle" : "Inc VAT"
    }
};

盡管可能有多個第二級“屬性”對象,但到目前為止,我有:

for (var key in jsonProductData['attributes']) {
    $j(jsonProductData.attributes[key].options.select(.id==7)
}

我知道這並不是特別遙遠,盡管我完全不知道該如何解決這個問題,因為我需要使用的id是對象中的值,該對象似乎不適用於此select函數。

有人能幫忙嗎?

這將從每個項目的options數組中獲取id

 var jsonProductData = { "attributes" : { "133": { "id":"133", "code":"size", "label":"Size", "options": [ {"id":"44","label":"XS","price":"0","oldPrice":"0","cssclass":"","products":["11921"]}, {"id":"9","label":"S","price":"0","oldPrice":"0","cssclass":"","products":["11922"]}, {"id":"8","label":"M","price":"0","oldPrice":"0","cssclass":"","products":["11923"]}, {"id":"7","label":"L","price":"0","oldPrice":"0","cssclass":"","products":["11924"]}, {"id":"6","label":"XL","price":"0","oldPrice":"0","cssclass":"","products":["11925"]}, {"id":"475","label":"XXL","price":"0","oldPrice":"0","cssclass":"","products":["11926"]} ] } }, "template" : "\£#{price}", "basePrice" : "187", "oldPrice" : "299.99", "productId" : "11950", "chooseText" : "Select Size...", "taxConfig" : { "includeTax":false,"showIncludeTax":true,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"Inc VAT" } }; var idToLookFor = 7; for (var key in jsonProductData.attributes) { var options = jsonProductData.attributes[key].options; for (var i=0; i < options.length; i++) { console.log('options[' + i + '].id = ' + options[i].id); var idAsInteger = parseInt(options[i].id); if (idToLookFor === idAsInteger) { var products = options[i].products; for (var j=0; j < products.length; j++) { console.log(' --> products[' + j + '] = ' + products[j]); } } } } 

您可以獲取這些嵌套對象的列表,然后找到具有特定ID的對象,如下所示:

 var jsonProductData={"attributes":{"133":{"id":"133","code":"size","label":"Size","options":[{"id":"44","label":"XS","price":"0","oldPrice":"0","cssclass":"","products":["11921"]},{"id":"9","label":"S","price":"0","oldPrice":"0","cssclass":"","products":["11922"]},{"id":"8","label":"M","price":"0","oldPrice":"0","cssclass":"","products":["11923"]},{"id":"7","label":"L","price":"0","oldPrice":"0","cssclass":"","products":["11924"]},{"id":"6","label":"XL","price":"0","oldPrice":"0","cssclass":"","products":["11925"]},{"id":"475","label":"XXL","price":"0","oldPrice":"0","cssclass":"","products":["11926"]}]}},"template":"\£#{price}","basePrice":"187","oldPrice":"299.99","productId":"11950","chooseText":"Select Size...","taxConfig":{"includeTax":false,"showIncludeTax":true,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"Inc VAT"}}; var arr = Object.keys(jsonProductData['attributes']).reduce( function(acc, key, i, attr) { return acc.concat(jsonProductData['attributes'][key].options); }, []); // find obj with number 7: var obj = arr.find(function (o) { return o.id == 7 }); // print it console.log(obj); // get product reference: console.log('found product:', obj.products[0]); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM