sortBy:

var collection = ['John', 'Petteri', 'Antti', 'Joonas', 'Zhentian'];
var sorted = _.sortBy(collection); //[ 'Antti', 'John', 'Joonas', 'Petteri', 'Zhentian' ]
var collection = ['John', 'Petteri', 'Antti', 'Joonas', 'Zhentian'];
var sorted = _.sortBy(collection).reverse(); //[ 'Zhentian', 'Petteri', 'Joonas', 'John', 'Antti' ]
var collection = [
{age: 90, name: "Zach"},
{age: 33,name: "Beth"},
{age: 8,name: "Yolanda"},
{age: 57,name: "Chris"},
{age: 80,name: "Abe"}
]; var sorted = _.sortBy(collection, "age"); /*
[ { age: 8, name: 'Yolanda' },
{ age: 33, name: 'Beth' },
{ age: 57, name: 'Chris' },
{ age: 80, name: 'Abe' },
{ age: 90, name: 'Zach' } ] */

sortedIndex:

var collection = [
{age: 90, name: "Zach"},
{age: 33,name: "Beth"},
{age: 8,name: "Yolanda"},
{age: 57,name: "Chris"},
{age: 80,name: "Abe"}
]; var newGuy = {age: 26, name: "Wan"}; var sortedCollection = _.sortBy(collection, "age");
console.log(sortedCollection); //Want to insert an new guy, first find his a position in the array
var index = _.sortedIndex(sortedCollection, newGuy, "age");
console.log(index); // //insert into the array.
sortedCollection.splice(index, 0, newGuy); /*
[ { age: 8, name: 'Yolanda' },
{ age: 26, name: 'Wan' },
{ age: 33, name: 'Beth' },
{ age: 57, name: 'Chris' },
{ age: 80, name: 'Abe' },
{ age: 90, name: 'Zach' } ]
*/

[Javascript + lodash] sortBy and sortedIndex的更多相关文章

  1. [Javascript] Lodash: Refactoring Simple For Loops (_.find, _.findLast, _.filter)

    This lesson shows how to refactor your old loops into using a simpler and more powerful lodash-style ...

  2. [Javascript] lodash: memoize() to improve the profermence

    Link: https://lodash.com/docs#memoize Example: .service('UserPresenter', function(UserConstants){ va ...

  3. npm lodash

    在数据操作时,Lodash 就是我的弹药库,不管遇到多复杂的数据结构都能用一些函数轻松拆解. ES6 中也新增了诸多新的对象函数,一些简单的项目中 ES6 就足够使用了,但还是会有例外的情况引用了少数 ...

  4. nodejs lodash的一些函数

    1   _.compact用法   _.compact([0, 1, false, 2, '', 3,'mm']); var test = _.compact([-1,0, 1, false, 2, ...

  5. underscore.js框架使用

    Underscore.js是一个很精干的库,压缩后只有4KB.它提供了几十种函数式编程的方法,弥补了标准库的不足,大大方便了JavaScript的编程.MVC框架Backbone.js就将这个库作为自 ...

  6. 小小改动帮你减少bundle.js文件体积(翻译)

    我已经从事过好多年的SPA开发工作,我发现很多的程序猿都从来不往 bundle.js 文件的体积上动脑筋,这让我有点懵逼. “安心洗路,等俺把代码混淆压缩后就一切666了”,若是有人这么说,我会翻白眼 ...

  7. underscore.js 一个强大的js函数库

    Underscore提供的100多个函数,主要涉及对Collection.Object.Array.Function的操作: Collections(集合) each, map, reduce, re ...

  8. webpack使用

    Webpack是一个现代js应用的模块打包机.如果一个文件依赖另一个文件,webpack认为这就存在一个依赖关系.不管另一个文件是什么内容,image,css或js都被当作一个模块.Webpack从e ...

  9. Currying 及应用

    Currying,中文多翻译为柯里化,感觉这个音译还没有达到类似 Humor 之于幽默的传神地步,后面直接使用 Currying. 什么是 Currying Currying 是这么一种机制,它将一个 ...

随机推荐

  1. Linux串口c_cc[VTIME]和c_cc[VMIN]属性设置的作用

    Linux串口c_cc[VTIME]和c_cc[VMIN]属性设置的作用 在串口编程模式下,open未设置O_NONBLOCK或O_NDELAY的情况下. c_cc[VTIME]和c_cc[VMIN] ...

  2. ASP.NET MVC:通过 FileResult 向 浏览器 发送文件

    在 Controller 中我们可以使用 FileResult 向客户端发送文件. FileResult FileResult 是一个抽象类,继承自 ActionResult.在 System.Web ...

  3. jQuery修改操作css属性实现方法

    在jquery中我们要动态的修改css属性我们只要使用css()方法就可以实现了,下面我来给各位同学详细介绍介绍. css()方法在使用上具有多样性,我们先来了解css()方法基本知识. css() ...

  4. Gen_event行为分析和实践

    1.简介 Gen_event实现了通用事件处理,通过其提供的标准接口方法以及回调函数,在OTP里面的事件处理模块是由一块通用的事件管理器和任意数量的事件处理器,并且这些事件处理器可以动态的添加和删除. ...

  5. C#中数据类型的安全转换(is,as)

    原文 C#中数据类型的安全转换(is,as) 下面代码中,不能装箱,在强制类型转换时出错,因为之前 c 是 class 类型,而却要把它转换为 int 类型,这是不可以的.虽然编译器能通过编译,但是 ...

  6. Clear All of Them I(HDU 3920状压dp)

    题意:给有2*n个敌人的位置,枪在(0,0)位置,一次能消灭两个敌人,耗费能量为枪到一个敌人,由这个敌人再到另个敌人的的距离和,求消灭所有敌人最小耗费能量. 分析:一次枚举状态的两位即可 #inclu ...

  7. 【转】Qt Creator在Windows上的调试器安装与配置

    https://www.librehat.com/qt-creator-on-windows-debugger-installation-and-configuration/

  8. js基础第四天

    多个tab栏切换class封装 <style>         *{margin:0;padding:0;}         ul{list-style:none;}         .b ...

  9. ACM2040

    关于亲和数的详细解释如下: http://www.kepu.net.cn/gb/basic/szsx/8/8_83/8_83_1004.htm /* 亲和数 时间限制:2000/1000 MS(JAV ...

  10. sql-定义变量

    declare @subject nvarchar(50) set @subject=(select Subject from dbo.Scores where ID=1) --select @sub ...