考虑一个包含以下数据的 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_special=true
放在第一位,按 #2 和 #3 排序。 - 将所有
typeone
放在typetwo
- 将所有
typetwo
放在所有其余的之前(或不带任何标志/类型)。
重要提示:当其中一些属性不存在或等于 ""
谢谢
最佳答案
我认为这不是最好的方法,但是有一种方法可以使用 Underscore 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
关于javascript - angular.js 使用优先级和对象属性值的 ng-repeat 排序过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25024256/