Lodash 中文文档 (v3.10.1) - “Math” 方法
Translated by
Original Docs:
“Math” 方法
_.add(augend, addend)
将两个数相加。
参数
augend
(number) : 待求和的第一个数值addend
(number) : 待求和的第二个数值
返回
(number) : 返回两数的和示例
_.add(6, 4);// → 10
_.ceil(n, [precision=0])
根据 precision
对 number
向上取整。
参数
n
(number) : 待向上取整的数值[precision=0]
(number) : 向上取整的精度
返回
(number) : 返回向上取整后的值
示例
_.ceil(4.006);// → 5_.ceil(6.004, 2);// → 6.01_.ceil(6040, -2);// → 6100
_.floor(n, [precision=0])
根据 precision
对 number
向下取整。
参数
n
(number) : 待向下取整的数值[precision=0]
(number) : 向下取整的精度
返回
(number) : 返回向下取整后的数值
示例
_.floor(4.006);// → 4_.floor(0.046, 2);// → 0.04_.floor(4060, -2);// → 4000
_.max(collection, [iteratee], [thisArg])
获取 collection
中的最大值。如果 collection
为空或是假值,则会返回 -Infinity
。如果提供了迭代器函数,那么其将被作用于 collection
中的每个值以来生成值排序的标准。iteratee
将绑定 thisArg
并在执行时传入三个参数:value
, index
, collection
。
如果提供的是属性名,那么 predicate
将创建 _.property
风格的回调函数,并返回给定元素的属性的值。
如果值还提供了 thisArg
,那么 predicate
将创建 _.matchesProperty
风格的回调,并在元素含有匹配的属性值的时候返回 true
,否则返回 false
。
如果提供的是对象,那么 predicate
将创建 _.matches
风格的回调函数,并在匹配给定对象的属性的元素时返回 true
,否则返回 false
。
参数
collection
(Array|Object|string) : 待迭代的集合[iteratee]
(Function|Object|string) : 每次迭代执行的函数[thisArg]
(*) :iteratee
绑定的this
返回
(*) : 返回最大值
示例
_.max([4, 2, 8, 6]);// → 8_.max([]);// → -Infinityvar users = [ { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }];_.max(users, function(chr) { return chr.age;});// → { 'user': 'fred', 'age': 40 }// 使用 `_.property` 回调函数简称_.max(users, 'age');// → { 'user': 'fred', 'age': 40 }
_.min(collection, [iteratee], [thisArg])
获取 collection
中的最小值。如果 collection
为空或是假值,则会返回 -Infinity
。如果提供了迭代器函数,那么其将被作用于 collection
中的每个值以来生成值排序的标准。iteratee
将绑定 thisArg
并在执行时传入三个参数:value
, index
, collection
。
如果提供的是属性名,那么 predicate
将创建 _.property
风格的回调函数,并返回给定元素的属性的值。
如果值还提供了 thisArg
,那么 predicate
将创建 _.matchesProperty
风格的回调,并在元素含有匹配的属性值的时候返回 true
,否则返回 false
。
如果提供的是对象,那么 predicate
将创建 _.matches
风格的回调函数,并在匹配给定对象的属性的元素时返回 true
,否则返回 false
。
参数
collection
(Array|Object|string) : 待迭代的集合[iteratee]
(Function|Object|string) : 每次迭代执行的函数[thisArg]
(*) :iteratee
绑定的this
返回
(*) : 返回最小值
示例
_.min([4, 2, 8, 6]);// → 2_.min([]);// → Infinityvar users = [ { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }];_.min(users, function(chr) { return chr.age;});// → { 'user': 'barney', 'age': 36 }// using the `_.property` callback shorthand_.min(users, 'age');// → { 'user': 'barney', 'age': 36 }
_.round(n, [precision=0])
按 precision
四舍五入 n
。
参数
n
(number) : 待计算的数值[precision=0]
(number) : 四舍五入的精度
返回
(number) : 返回四舍五入后的数值示例
_.round(4.006);// → 4_.round(4.006, 2);// → 4.01_.round(4060, -2);// → 4100
_.sum(collection, [iteratee], [thisArg])
Gets the sum of the values in collection.
获取 collection
的值的和
参数
collection
(Array|Object|string) : 待迭代的集合[iteratee]
(Function|Object|string) : 每次迭代执行的函数[thisArg]
(*) :iteratee
绑定的this
返回
(number) : 返回计算的和
示例
_.sum([4, 6]);// → 10_.sum({ 'a': 4, 'b': 6 });// → 10var objects = [ { 'n': 4 }, { 'n': 6 }];_.sum(objects, function(object) { return object.n;});// → 10// 使用 `_.property` 回调函数简称_.sum(objects, 'n');// → 10