js lodash 'date' 'nunber' 'math' methods

“Date” Methods

now

_now()

返回从1970/07/01 00:00:00 到现在过去了的毫秒数。

Example
console.log(_.now());
//=>1435928295415 现在的时间是2015/07/03 20:58:xx

‘Number’ merhods

inRange

_.inRange(n, [start=0], end)

检查n这个数值是否在start和end(不包括end)之间,前闭后开区间。如果end
这个值没有被指定,就把end设为start的值,star设为0.

Arguments
  • n (number): 要检查的数.

  • [start=0] (number): 区间的开始值.

  • end (number): 区间的结束值.

Returns

(boolean): n如果在区间中,返回true,否则返回false.

Example
console.log(_.inRange(2,1,4));
//=>true
console.log(_.inRange(4,4));
//=>false  4不在[0,4)之间

random

_.random([min=0], [max=1], [floating])

产生一个[min,max]的随机数. 如果min和max只有一个值给定,那么返回0到这个值之间的一个随机数。如果floating是true,或者min和max都是floats,那么返回的是一个浮点数而不是一个整数。

Arguments
  • [min=0] (number): 可能产生的最小值.

  • [max=1] (number): 可能产生的最大值.

  • [floating] (boolean): 是一个布尔值,返回一个浮点数.

Returns

(number): 返回一个随机数.

Example
console.log(_.random(24));
//产生一个0到24的整数
console.log(_.random(2.1,24.2));
//产生一个2.1到24.2的浮点数
console.log(_.random(0,24,true));
//产生一个0到24的浮点数

‘Math’ methods

add

_.add(augend, addend)

将两个数相加.

Arguments
  • augend (number): 要相加的第一个数.

  • addend (number): 要相加的第二个数.

Returns

(number): 返回和.

Example
console.log(_.add(1,2));
//=> 3

ceil

_.ceil(n, [precision=0])

将数n上舍入到指定的精度precision。

Arguments
  • n (number): 要上舍入的数.

  • [precision=0] (number): 要上舍入的精度.

Returns

(number): 返回上舍入后的值.

Example
console.log(_.ceil(4.123456,3));
//=>4.124
console.log(_.ceil(123456,-2));
//=>123500

floor

_.floor(n, [precision=0])

将数n下舍人到指定的精度precision.

Arguments
  • n (number): 要下舍入的数.

  • [precision=0] (number): 要下舍人的精度.

Returns

(number): 返回下舍入之后的数.

Example
console.log(_.floor(4.123456,3));
//=>4.123
console.log(_.floor(123456,-2));
//=>123400

max

_.max(collection, [iteratee], [thisArg])

返回集合中最大的数。如果集合是空或者错值,返回-Infinity。如果给定了iteratee,对集合中的每一个值都调用iteratee,返回的集合根据集合中数值来排序。iteratee与thisArg绑定并且可以调用三个参数: (value, index, collection).

如果iteratee是一个对象,如果这个对象能在collection中找到,那么返回这个对象,如果这个对象在collection中找不到,那么返回collection中的第一个对象。

如果iteratee是一个属性名,取集合中相应属性名的属性值最大的对象,返回这个对象.

如果iteratee是一个属性名,并且thisArg也有值,情况和iteratee是一个对象的情况是一样的。

Arguments
  • collection (Array|Object|string): 要遍历的集合.

  • [iteratee] (Function|Object|string): 对每个元素都要调用的函数.

  • [thisArg] (*): 与iteratee绑定的值.

Returns

(*): 返回最大值.

Example
var aas=[{aa:'a','age':12},{aa:'s','age':23},{aa:'d','age':45}];
console.log(_.max(aas,{aa:'s','age':24}));
//=>{aa:'a','age':12}
console.log(_.max(aas,'age'));
//=>{ aa: 'd', age: 45 }
console.log(_.max(aas,'age',13));
//=>{ aa: 'a', age: 12 }

min

_.min(collection, [iteratee], [thisArg])

返回集合中的最小值.如果iteratee是一个函数,对collection中的每个值调用iteratee,返回true时,在根据数值的排序来返回值. iteratee与thisArg绑定并且可以调用三种参数: (value, index, collection).

如果iteratee是一个对象,如果这个对象能在collection中找到,那么返回这个对象,如果这个对象在collection中找不到,那么返回collection中的第一个对象。

如果iteratee是一个属性名,取集合中相应属性名的属性值最小的对象,返回这个对象.

如果iteratee是一个属性名,并且thisArg也有值,情况和iteratee是一个对象的情况是一样的。

Arguments
  • collection (Array|Object|string): 要遍历的集合.

  • [iteratee] (Function|Object|string): 对每个元素调用.

  • [thisArg] (*): 与iteratee绑定的值.

Returns

(*): 返回最小值.

Example

var aas=[{aa:'a','age':23},{aa:'s','age':12},{aa:'d','age':45}];
console.log(_.min(aas,{aa:'s','age':24}));
//=>{ aa: 'a', age: 23 }
console.log(_.min(aas,'age'));
//=>{ aa: 's', age: 12 }
console.log(_.min(aas,'age',13));
//=>{ aa: 'a', age: 23 }

round

_.round(n, [precision=0])

对数n进行四舍五入到指定的精度precision.

Arguments

n (number): 要四舍五入的数.
[precision=0] (number): 要四舍五入到的精度.

Returns

(number): 返回四舍五入后的数.

Example
console.log(_.round(4.32,1));
//=>4.3
console.log(_.round(4.35,1));
//=>4.4

sum

_.sum(collection, [iteratee], [thisArg])

返回collection中数值的和。

Arguments
  • collection (Array|Object|string): 要遍历的集合.

  • [iteratee] (Function|Object|string): 对每个元素都进行调用.

  • [thisArg] (*): 与iteratee绑定的值.

Returns

(number): 返回和.

Example
console.log(_.sum([1,2]));
//=>3
console.log(_.sum([{'a':1},{'a':2},{'a':3}],'a'));
//=>6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值