如何访问复杂结构的数据

How to access data in complex structure?

本文关键字:复杂 结构 数据 访问 何访问      更新时间:2023-09-26

我试图整理我的网站的愿望列表功能,这样做,我需要从另一个数组中的数组调用一个值,我需要得到底层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" : "'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" }
};
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":"'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"}};
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; }