UnderscoreAPI阅读笔记

Collections部分

今天来看一下underscore的API,总之这是一个非常强大的库,用起来非常优雅~

_.each(list,iteratee,[context])

forEach只适用于Array对象,当然也可以使用_.each(arr,func)来实现
但是如果是引用类型的话,就必须使用_.each(obj,func),而且传递给func的参数是(value, key, list)

var obj = {
      name:"Daming",
      age:15
}
_.each(obj,function () {
      console.log(arguments);
})

打印结果

{ '0': 'Daming', '1': 'name', '2': { name: 'Daming', age: 15 } }
{ '0': 15, '1': 'age', '2': { name: 'Daming', age: 15 } }

可以看到,第一行是第一个键值对name:”Daming”的值、Key,最后一个就是完整的Obj,第二行也类似。可见,在引用类型进行迭代的时候用这种办法就比较方便。

_.find(list,predicate,[context])

只要找到第一个符合要求的就返回。因此不会全部循环。

_.where(list,properties)

返回一个数组,这个数组里的函数具有properties所写的键值对

_.where(listOfPlays, {author: "Shakespeare", year: 1611});
=> [{title: "Cymbeline", author: "Shakespeare", year: 1611},
    {title: "The Tempest", author: "Shakespeare", year: 1611}]

_.findWhere

和where差不多啦,就是返回第一个

_.reject(list,predicate,[context])

顾名思义,也就是剔除某些数组内容。

_.every(list,[predicate],[context])

貌似不传predicate的话就是true。这个的作用就是如果每一个的返回值都是真,那么就返回真,和some相反。some的话就是如果有真的,返回结果就是真。

_.contains(list,value,[fromIndex])

官方说这个内部实现是通过_.indexOf()来实现的。如果有fromIndex的话就从这个位置开始寻找。

_.invoke(list,methodName,*arguments)

对每一个list的内容都执行methodName函数,如果有参数需要传递的话。可以在从第三个参数的位置开始进行填写。注意哟。这个methodName也可以是list中内容的方法哟。看下面的例子

function People(name,age){
      this.name = name
      this.age = age
}
People.prototype.sayName = function(){
      return this.name
};
var persons = []
var p1 = new People("Daming",12)
var p2 = new People("Amy",13)
var p3 = new People("Simon",13)
persons.push(p1)
persons.push(p2)
persons.push(p3)
// var res = _.contains(con,{name:"Daming3"});
//
var res = _.invoke(persons,"sayName")
console.log(res);//[ 'Daming', 'Amy', 'Simon' ]

_.pluck(list,propertyName)

提取出List中每一个的propertyName属性,组成数组,个人觉得这个功能很赞啊

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.pluck(stooges, 'name');
=> ["moe", "larry", "curly"]

_.max|min(list,[iteratee],[context])

根据提供的Iteratee或者默认的排列大小的来进行选择最大的,注意如果是空数组,将返回-Infinity。所以最好是使用之前先判断是不是空数组

_.sortBy(list,iteratee,[context])

感觉这个也是Array功能的拓展,如果List不是数组的话可以用这个办法来使用。

_.groupBy(list,iteratee,[context])

这个太强大了。。。。。。。如果iteratee是函数,可以根据Iteratee的运行返回值来进行分组。。。。。。。如果iteratee不是函数的话,就将它作为item的属性来进行分组。

_.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
=> {1: [1.3], 2: [2.1, 2.4]}

_.groupBy(['one', 'two', 'three'], 'length');
=> {3: ["one", "two"], 5: ["three"]}

_.indexBy(list,iteratee,[context])

就是groupBy的山寨品,不过这个函数在分组的时候,一个组只能有一个item哦。也就是说这个作为分组的属性必须是unique的

_.countBy(list,iteratee,[context])

这个功能也是相当强大啊,不信你看

var persons = [];
persons.push(new People("Daming",12));
persons.push(new People("Amy",13));
persons.push(new People("Daming",13));
persons.push(new People("Tony",18));
persons.push(new People("David",21));
// var res = _.contains(con,{name:"Daming3"});
//
console.log(
      _.countBy(persons,function(val){
            if(val.age>=18)return 'adult'
            else return 'teenager'
      })
)
//{ teenager: 3, adult: 2 }

_.shuffle(list)

顾名思义,就是打乱

_.sample(list,[num])

从list中随机取出一些数字,如果num不指定的话,取出的数字的数目也是随机的。


剩下的以后再写~

Arrays部分

Functions部分

Objects部分

Utility部分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值