Javascript angular.js使用优先级和对象属性值的ng repeat排序过滤器

Javascript angular.js使用优先级和对象属性值的ng repeat排序过滤器,javascript,json,angularjs,Javascript,Json,Angularjs,考虑一个包含以下数据的JSON对象: { "results": [ { flags: [ is_special: true // or false, or non-existing ], type: "typeone" // or typetwo, or non-existing }, ... // repeats ] } 为排序结果的ng repeat=“resu

考虑一个包含以下数据的JSON对象:

{
   "results": [
      { 
         flags: [
            is_special: true // or false, or non-existing
         ],

         type: "typeone" // or typetwo, or non-existing
      },
      ... // repeats
   ]
}
为排序结果的
ng repeat=“result in json.results”
创建过滤器的有效方法是什么:

  • 将所有的
    is#u special=true
    放在第一位,由#2和#3排序
  • 将所有
    typeone
    放在
    typetwo
  • 通过将所有
    typetwo
    放在所有其他标记/类型之前(或不带任何标记/类型)
  • 重要提示:当其中一些属性不存在或等于


    谢谢

    我不认为这是最好的方法,但有一种方法可以用下划线JS实现这一点。希望有人能优化我下面的代码:

    // Grab the items that have the flag
    first = _.filter(results, function(r)
    {
        return r.flags.is_special;
    }
    
    // Grab the remainder
    remainder = _.where(results, {flags.is_special: false});
    
    // Now grab your second list
    second = _.filter(remainder, function(r)
    {
        return r.type =='typeone';
    });
    
    // Again grab the remainder (and so on)
    
    // Then return your 4 lists
    

    orderBy
    接受一个参数数组,因此您可以直接进入列表:

    ng-repeat="result in json.results | orderBy: [isSpecial, typeFilter]"
    
    
    $scope.isSpecial = function(item) {
        for (var i = 0; i < item.flags.length; i++) {
           if (item.flags[i]["is_special"])
                return true;
        }
    }
    
    $scope.typeFilter = function(item) {
        item.type == "typeone" ? return 1 : return 0;
    }
    
    ng repeat=“result in json.results | orderBy:[isSpecial,typeFilter]”
    $scope.isSpecial=功能(项目){
    对于(变量i=0;i
    标志是否总是只有一项?否@tymeJV,可以有更多。我还加了一个新的约束,我想这就是答案。但是这两个函数需要做一些工作
    isSpecial
    应该为is_special返回0,否则可能返回1
    typeFilter
    可能会为typeone返回0,为typetwo返回1,为rest返回2。