简体   繁体   English

如何访问复杂结构的数据?

[英]How to access data in complex structure?

I'm trying to sort out my site's wishlist function and to do so I need to call a value from an array inside another array, I need to get the bottom level products object value, depending on which 4th level options object is needed (dependent on the 4th level id (44, 9, 8, 7, 6, 475 in this case)): 我正在尝试整理网站的心愿单功能,为此,我需要从另一个数组中的一个数组中调用一个值,我需要获取底层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"
    }
};

Although there may be multiple 2nd level 'attribute' objects, so so far I have: 尽管可能有多个第二级“属性”对象,但到目前为止,我有:

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

I know this isn't particularly far although I'm at a complete loss how to get past this, as the id I need to use is a value in an object, which doesn't seem to work with this select function. 我知道这并不是特别遥远,尽管我完全不知道该如何解决这个问题,因为我需要使用的id是对象中的值,该对象似乎不适用于此select函数。

Anyone able to help? 有人能帮忙吗?

This will get the id from inside the options array for each item: 这将从每个项目的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]); } } } } 

You could get a list of those nested objects, and then find the one with a particular id as follows: 您可以获取这些嵌套对象的列表,然后找到具有特定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