如何访问复杂结构的数据
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" : "'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; }
相关文章:
- 更改JSON对象的结构
- 复杂的 JSON 结构 + 道场选择
- JSrender- 从复杂结构访问元素
- AngularJS ng repeat指令,具有复杂的嵌套数据结构和动态键
- 复杂的结构化 JSON 迭代
- 在 jsp 页中设计一个复杂的结构,并使用操作类在其上传达值
- 如何访问复杂结构的数据
- 复杂JSON结构
- jsonRest复杂结构与dojo
- 如何使用简单模式描述这种复杂的文档结构
- 在谷歌浏览器上从一帧到一帧的复杂框架结构(3色,每行3行)
- 需要将复杂的json对象转换为合适的angularjs UI树数据json结构
- 用于复杂应用结构的NodeJS本地模块
- 使用JQuery从复杂的html结构中获取元素
- React JS -解析一个复杂的Json结构并在下拉列表中填充
- jquery每个迭代复杂的JSON结构与“where子句”
- 在jQuery/JS中生成复杂html层次结构的最佳方法
- 如何处理复杂数据结构的列表
- 将复杂的层次结构推送到可观察数组中会创建重复的实例
- 对复杂的类结构和Knockout感到困惑