对 angular 中的多维 JSON 进行排序

Sort multidimensional JSON in angular

我从数据库中得到JSON,它的结构是这样的:

  "data": [
    {
      "category": "1",
      "description": "hello"
      }
   ]

descriptioncategory 都是动态的,所以我可以有 5 个对象 category:1 和 2 个对象 category:3 等。或者我可以有 3 个对象 category:20 和 25 个带有 category:8.

的对象

我想创建 HTML 个这样的元素:

Category 1
....
....
....

Category 3
....
....

我可以这样创建一个字符串:

$scope.hello = [];
angular.forEach(data.data, function(value, key) {
  $scope.hello.push(value.category+" "+value.description)
}

这样它会输出:

Category 1 hello
Category 1 hi
Category 1 bye
Category 3 sup
Category 3 yo

但我将如何把它变成:

Category 1
hello 
hi
bye

Category 3
sup
yo

javascript 提供了 sort 功能。在此函数中,我通过两个属性对站点对象数组进行排序;名称和地区。

    function sort(col, direction) {
        switch (col) {
            case "region":
            {
                sites.sort(function(s1, s2) {
                    var r = 0;
                    if (s1.region < s2.region)
                        r = -1;
                    else if (s1.region > s2.region)
                        r = 1;
                    else {
                        if (s1.name < s2.name)
                            r = -1;
                        else if (s1.name > s2.name)
                            r = 1;
                    }
                    return r;
                });
                break;
            }
            default:
            {
                sites.sort(function(s1, s2) {
                    var r = 0;
                    if (s1.name < s2.name)
                        r = -1;
                    else if (s1.name > s2.name)
                        r = 1;
                    else {
                        if (s1.region < s2.region)
                            r = -1;
                        else if (s1.region > s2.region)
                            r = 1;
                    }
                    return r;
                });
            }
        }
        if (direction === "desc") {
            this.sites.reverse();
        }
    }

使用 angular.filter 模块的 groupBy 了解更多信息,请参阅此答案。 How can I group data with an Angular filter?

var app = angular.module("app",['angular.filter'])
app.controller('ctrl',['$scope', function($scope){
        $scope.data = [
                {
                  "category": "1",
                  "description": "hello"
                },
          {
                  "category": "1",
                  "description": "hi"
                },
          {
                  "category": "3",
                  "description": "hai"
                },
          {
                  "category": "1",
                  "description": "hello"
                },
          {
                  "category": "3",
                  "description": "hello"
                },
          {
                  "category": "1",
                  "description": "hai"
                }
   ]
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div class="item item-checkbox">
   <ul ng-repeat="(key, value) in data | groupBy: 'category'">
      category: {{ key }}
      <li ng-repeat="d in value |orderBy:'description'">
          {{ d.description }} 
      </li>
   </ul>      
</div>