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. volicety常用方法

    1.volicety得到某个元素的个数 $extendsInfos.size() 2.volicety 布尔值判断: 如果a为null,#if($a) ,产生的判断值是false  等同于#if(fa ...

  2. 陈正冲老师讲c语言之const关键字

    1.const 关键字也许该被替换为 readolny const是constant的缩写,是恒定不变的意思,也翻译为常量.常数等.很不幸,正是因为这一点,很多人都认为被const修饰的值是常量.这是 ...

  3. BingWallpaper

    桌面壁纸更换成Bing.com的每日图片 项目地址:https://github.com/atskyline/BingWallpaper 其实就只是一个脚本,只是觉得二进制文件使用比较方便,所以采用C ...

  4. (八)学习CSS之line-style-type属性

    参考:http://www.w3school.com.cn/cssref/pr_list-style-type.asp 设置不同的列表样式: ul.circle {list-style-type:ci ...

  5. vtiger 支持 物业收费功能 微信收费

    谁要?需要什么功能? 直接在下面留言,博主会整理大家的需求,形成产品,发出来.

  6. Using SharePoint 2010 dialogs

    转:http://www.techbubbles.com/sharepoint/using-sharepoint-2010-dialogs/ SharePoint 2010 dialogs are J ...

  7. MySQL SQL优化之字符串索引隐式转换

    之前有用户很不解:SQL语句非常简单,就是select * from test_1 where user_id=1 这种类型,而且user_id上已经建立索引了,怎么还是查询很慢? test_1的表结 ...

  8. Android 退出提示框 代码

    转自:http://hi.baidu.com/ittdt/item/d932cf37f486f886c3cf29ea new AlertDialog.Builder(MainEngine.contex ...

  9. selenium Grid

    Selenium Grid 的机制是启动一个 hub,然后启动多个 Selenium RC 注册到 hub 上, 当测试请求到 hub 时,hub 会将测试分发给 Selenium RC, Selen ...

  10. codeforce 606B Testing Robots

    题意:给定一个x*y的矩形,和一个机器人的初始位置(x0,y0).以向下为x轴正方向,向右为y轴正方向.现在要对这个机器人进行多次测试.每次测 试,会在矩形的某个位置有一个矿井.所以一共要进行x*y次 ...