javascript - AngularJS 自定义顺序和限制

标签 javascript arrays angularjs sorting

我有项目列表,需要按优先级将它们分类到列表中。

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/

相关文章:

javascript - jQuery UI 可选 : how to get mouse coordinates on 'stop' event?

javascript - 如何在 Microsoft edge 中获取选中的单选按钮的值?

javascript - 比较两个数组以检索值

java - 将文件内容存储到数组中

javascript - 如何在新页面中显示选中的项目?

javascript - 如何从用冒号分隔的角色字符串中获取数据?

javascript - NodeJS 只是挂起,没有错误,没有崩溃

javascript - 检查是否可以在另一个数组中找到所有项目

javascript - Angular,在 $get 中调用新的构造函数(在提供者中)

jquery - AngularJS 和指令的初学者问题 - 包装 jQuery 组件