Edward Edward - 1 month ago 15
Javascript Question

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)):

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.

Anyone able to help?

Answer

This will get the id from inside the options array for each item:

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]);
      }
    }
  }
}