gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:58:46 25 4
gpt4 key购买 nike

考虑一个包含以下数据的 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" 创建过滤器的有效方法是什么:

  1. 将所有 is_special=true 放在第一位,按 #2 和 #3 排序。
  2. 将所有typeone放在typetwo之前
  3. 通过将所有 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 - 使用优先级和对象属性值的 ng-repeat 的 angular.js 排序过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25024256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com