如何移动所有的零值与LoDash/下划线数组的结束
How to move all zero values to the end of the array with LoDash/Underscore?
假设我有一个对象数组:
var a = [
{label: 'one', value: 0},
{label: 'two', value: 5},
{label: 'three', value: 9},
{label: 'four', value: 4},
{label: 'five', value: 2},
{label: 'six', value: 0}
]
现在我需要把所有的项按升序排列这在_.sortBy
中很容易做到,但然后我想把从数组开始到末尾的所有零值,所以我的值行看起来像2, 4, 5, 9, 0, 0
?
您可以使用自定义迭代器和Infinity属性来表示0的值,从而将它们推到末尾:
var s = _.sortBy(a, function(o) {
return o.value || Infinity ;
});
http://jsfiddle.net/nikoshr/LwYpq/你可以用下划线库来做,像这样
var _ = require("underscore");
var result = _.partition(a, function(currentObject) {
return currentObject.value !== 0;
});
console.log(_.flatten([_.sortBy(result[0], "value"), result[1]]));
相关文章:
- 使用lodash下划线基于键拆分jsonarray
- 在Backbone.js应用程序中使用lodash而不是下划线
- lodash/下划线 - 使用绑定与方法调用和方法保持未绑定
- 下划线where with or condition(下划线、lodash或任何其他解决方案)
- Lodash/下划线函数,用于使用给定长度的默认空值初始化数组
- 如何修剪在 LoDash/下划线中开始多个字符串
- 下划线和 LoDash _.each
- 防止前端的 lodash 和下划线之间的冲突
- 如何在具有冲突库的网站上正确包含 lodash/下划线
- Lodash & Bower:指定下划线“插入替换”版本的最佳方法
- 使用lodash/下划线聚合排序限制
- 使用下划线js或lodash将对象解析为数组
- 使用下划线/Lodash分组By和Sum
- 使用下划线和lodash库按ID从数组中获取项
- 如何移动所有的零值与LoDash/下划线数组的结束
- 使用下划线或Lodash将行数组简化为汇总汇总行
- 使用下划线(或Lodash),如何从另一个数组中删除数组的每个成员
- LoDash /下划线不是全局变量
- 以函数风格(下划线/Lodash)对对象进行分块
- 如何分割javascript数组的对象与特定条件使用lodash/下划线