我有如下的json数据[{ "id": 1, "address": "MG Road", "country": INDIA, "state": AP, "city": VIJ},{ "id": 2, "address": "Miyapur", "country": INDIA, "state": TS, "city": HYD},{ "id": 3, "address": "Autonagar", "country": INDIA, "state": AP, "city": VIJ},{ "id": 4, "address": "Kukatpalli", "country": INDIA, "state": TS, "city": HYD},{ "id": 5, "address": "Koti", "country": INDIA, "state": TS, "city": HYD}]
我想以以下格式显示
IND,TS,HYD
宫古,科蒂,库卡帕特里
IND,AP,VIJ,
MG路,Autonagar
为此我使用如下的groupBy过滤器,但我无法将这些值分组
这是我的代码
<div class="location-container">
<label ng-repeat="(key,value) in locationmodel | groupBy: '[country,state,city]'">{{key}}
</label>
<span ng-repeat="location in value">{{location.address}}</span>
</div>
但是使用上面的代码,我无法达到我的期望。请帮助我解决这个问题。
提前致谢
最佳答案
如果您愿意,可以尝试使用此方法获得输出
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var myApp = angular.module('myApp', []);
myApp.controller('myAppCtrl', function ($scope) {
var locationmodel = [{
"id" : 1,
"address" : "MG Road",
"country" : "INDIA",
"state" : "AP",
"city" : "VIJ"
}, {
"id" : 2,
"address" : "Miyapur",
"country" : "INDIA",
"state" : "TS",
"city" : "HYD"
}, {
"id" : 3,
"address" : "Autonagar",
"country" : "INDIA",
"state" : "AP",
"city" : "VIJ"
}, {
"id" : 4,
"address" : "Kukatpalli",
"country" : "INDIA",
"state" : "TS",
"city" : "HYD"
}, {
"id" : 5,
"address" : "Koti",
"country" : "INDIA",
"state" : "TS",
"city" : "HYD"
}
];
var resultObj = {};
for (var i = 0; i < locationmodel.length; i++) {
if (resultObj[locationmodel[i].country + ',' + locationmodel[i].state + ',' + locationmodel[i].city])
resultObj[locationmodel[i].country + ',' + locationmodel[i].state + ',' + locationmodel[i].city].address.push(locationmodel[i].address);
else
resultObj[locationmodel[i].country + ',' + locationmodel[i].state + ',' + locationmodel[i].city] = {
address : [locationmodel[i].address]
};
}
$scope.result=resultObj;
});
</script>
<body ng-app="myApp" ng-controller='myAppCtrl'>
<div class="location-container" ng-repeat="(key,value) in result">
<label >{{key}}</label><br>
<span>{{value.address.join()}}</span>
</div>
</body>