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. Datalist增删改查——联系人管理

    关于Datalist,其实和Repeater差不多,都是存放数据的控件,相比较下,Datalist和Repeater虽然都是用的模板,但是Datalist比之多了Edit模板,也就是编辑栏的模板,事件 ...

  2. poj2376

    最少区间覆盖问题: 首先我们想到将r排序,则以得出dp方程 f[i]=1 (l[i]=1) =min{f[j]}+1 (r[j]+1>=l[i]) 最后ans是min{f[j]} (r[j]&g ...

  3. Gentoo源码安装图解

    Gentoo源码安装 一.前期准备 (1)下载以下三个文件 二.配置安装环境 (1)用光盘引导到LiveCD环境 (2)配置当前LiveCD环境的网络 安装Gentoo时,服务器引导的LiveCD环境 ...

  4. 微软 Virtual studion Code

    在 Build 2015 大会上,微软除了发布了 Microsoft Edge 浏览器和新的 Windows 10 预览版外,最大的惊喜莫过于宣布推出免费跨平台的 Visual Studio Code ...

  5. 服务器上的iptables

    服务器上的iptables 防火墙设置脚本规则 完整脚本如下: 复制代码代码示例: #!/bin/bash# by www.jbxue.comiptab="/sbin/iptables&qu ...

  6. 多线程程序设计学习(11)Two-phapse-Termination pattern

    Two-phapse-Termination[A终止B线程] 一:Two-phapse-Termination的参与者--->A线程--->B线程 二:Two-phapse-Termina ...

  7. (六)学习MVC之标签a提交页面

    标签<a>如何做到与<input type="submit"/>一样有提交页面信息的效果? @using (Html.BeginForm("Log ...

  8. java产生随机数的几种方式(转)

    一.在j2se里我们可以使用Math.random()方法来产生一个随机数,这个产生的随机数是0-1之间的一个double,我们可以把他乘以一定的数,比如说乘以100,他就是个100以内的随机,这个在 ...

  9. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.5

    Suppose it is known that $\scrM$ is an invariant subspace for $A$. What invariant subspaces for $A\o ...

  10. 设计模式_Mediator_调停者模式

    形象例子: 四个MM打麻将,相互之间谁应该给谁多少钱算不清楚了,幸亏当时我在旁边,按照各自的筹码数算钱,赚了钱的从我这里拿,赔了钱的也付给我,一切就OK啦,俺得到了四个MM的电话.调停者模式: 调停者 ...