我有项目列表,需要按优先级将它们分类到列表中。
items = [
{'type': 2, 'priority': 1, 'name': 'one'},
{'type': 1, 'priority': 2, 'name': 'two'},
{'type': 1, 'priority': 3, 'name': 'three'},
{'type': 1, 'priority': 4, 'name': 'four'},
{'type': 1, 'priority': 5, 'name': 'five'},
{'type': 2, 'priority': 6, 'name': 'six'},
]
我需要使用 ng-repeat
按优先级对它们进行排序,并按类型分开。在一个列表中,type
值的最大总和应为 4。因此输出应类似于(按 name
)
['one', 'two', 'three', 'four']
['five', 'six']
最佳答案
与 Underscorejs你可以试试:
var newItems = _.chain(items).sortBy('priority').groupBy('type').value();
最后,您可以在 ng-repeat 中遍历新数组。
编辑:这是 jsfiddle:http://jsfiddle.net/wb5d5pfs/
关于javascript - AngularJS 自定义顺序和限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30908445/