我正在尝试整理我的网站的wishlist函数,为此我需要从另一个数组中的数组中调用一个值,我需要获取底层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函数
任何人都可以提供帮助吗?
答案 0 :(得分:0)
这将从id
数组中为每个项目获取options
:
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]);
}
}
}
}
答案 1 :(得分:0)
您可以获取这些嵌套对象的列表,然后找到具有特定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]);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;