[英]Angularjs filter complex JSON by array
I'm having a dropdown list with multiselect option. 我有一个带有multiselect选项的下拉列表。 I want to filter out data from a complex JSON based on that array.
我想从基于该数组的复杂JSON过滤掉数据。
Selected options forms a array of data like: 选定的选项形成一个数据数组,例如:
$scope.myval=["Adyar","Paris","central"];
My JSON : 我的JSON:
$scope.myTest={
"buslist":
{
"code":"1",
"message":"Success",
"fromStationCode":"71",
"searchResult":[ {
"arrivalTime":"17:00:00",
"availableSeats":"42",
"boardingPointDetails":[{
"code":"1631",
"name":"Koyambedu",
"time":"09:30:00"
},
{
"code":"961296",
"name":"Paris",
"time":"09:45:00"
}
]
]
},
{
"arrivalTime":"18:00:00",
"availableSeats":"32",
"boardingPointDetails":[{
"code":"2084",
"name":"Adyar",
"time":"09:30:00"
},
{
"code":"961296",
"name":"Madurai",
"time":"09:45:00"
}
]
]
}
}
...
};
My HTML templating is: 我的HTML模板是:
<tbody ng-repeat=" i in myTest.buslist.searchResult" >
<tr>
<td>{{i.arrivalTime}}</td>
<td>{{i.availableSeats}}</td>
<td>
<p ng-repeat="m in i.boardingPointDetails">{{m.name}}</p>
</td>
</tr>
</tbody>
I want to filter my data based on selected values. 我想根据选定的值过滤数据。 I had tried something like this :
我尝试过这样的事情:
$scope.matched = $scope.myTest.buslist.searchResult.boardingPointDetails.name.indexOf(data);
ie:selected options must match "name" field in "boardingPointDetails" but it fails. ie:selected选项必须与“ boardingPointDetails”中的“名称”字段匹配,但失败。 Thanks in advance.
提前致谢。
Since $scope.myTest.buslist.searchResult.boardingPointDetails
is an array $scope.myTest.buslist.searchResult.boardingPointDetails.name
is not valid. 由于
$scope.myTest.buslist.searchResult.boardingPointDetails
是一个数组,因此$scope.myTest.buslist.searchResult.boardingPointDetails.name
无效。
You need to use an Array function to get the correct result: 您需要使用Array函数以获得正确的结果:
$scope.matched = $scope.myTest.buslist.searchResult.boardingPointDetails.filter(function(el) {
return el.name === data;
}).length > 0;
EDIT: 编辑:
Due to your comments I understand you want to get the boardPointDetails
that has the same name
property as one of the data
options. 根据您的评论,我了解您希望获得与
data
选项之一具有相同name
属性的boardPointDetails
。 Where data
is an Array of strings. 其中
data
是字符串数组。
This will do the job: 这将完成工作:
$scope.matched = $scope.myTest.buslist.searchResult.boardingPointDetails.filter(function(el) {
return data.indexOf(el.name) === 1;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.