【发布时间】:2015-02-13 12:11:05
【问题描述】:
我正在尝试搜索 json 对象以选择一些值。例如,我有一个值为 'product-2' 的变量,我想查看 json 对象并返回 'product-2 的 attributes 数组'
{
"attributes": [
...
],
"portfolio": [
{
"conn": [
{
"product": "product-1",
"description": "Description in here",
"attributes": [
"OriginPostcode",
"Size",
"Bandwidth"
],
},
{
"product": "product-2",
"description": "Description in here"
"attributes": [
"OriginPostcode",
"Size",
"Bandwidth"
],
}
]
}
]
谁能告诉我如何做到这一点?谢谢
编辑:
根据 Pramods 的要求 - 我正在使用以下 js(尽管我确信它确实是错误的)
$scope.productAttributes = [];
$scope.getProductDetails = function (product_id) {
console.log(product_id);
//search trough json
angular.forEach($scope.listOfProducts.product_id, function(value, key) {
// I was thinking I could loop through the json and when I find the matching product, then push its attributes into an array?
// if (key === enteredValue) {
// $scope.productAttributes.push({atribute: key});
// }
});
};
编辑二号
JSON 结构发生了变化
【问题讨论】:
-
请分享您到目前为止所尝试的内容。
-
我认为你必须使用`portfolio.conn[1].product-2.attributes`如果工作而不是问我我会评论它,因为你必须接受这些答案
-
您可以尝试使用 JPath :s-anand.net/blog/jpath-xpath-for-javascript
-
嘿 Gunjan - 我试过了,我已经在使用 allProducts.data.portfolio[0].conn 进入产品列表。但我不知道如何通过 conn 搜索“product-2”并将其属性加载到数组中
标签: javascript json angularjs