主题
meanBy
js
_.meanBy(array, [iteratee=_.identity])
此方法与 _.mean
类似,不同之处在于它接受 iteratee
,调用 array
中的每个元素生成要平均的值。迭代器使用一个参数调用:(value)。
¥This method is like _.mean
except that it accepts iteratee
which is invoked for each element in array
to generate the value to be averaged. The iteratee is invoked with one argument: (value).
新增于
¥Since
4.7.0
参数
¥Arguments
array
(数组):要迭代的数组。¥
array
(Array): The array to iterate over.[iteratee=_.identity]
(函数):每个元素调用迭代器。¥
[iteratee=_.identity]
(Function): The iteratee invoked per element.
返回
¥Returns
(数值):返回平均值。
¥(number): Returns the mean.
示例
¥Example
js
var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
_.meanBy(objects, function(o) { return o.n; });
// => 5
// The `_.property` iteratee shorthand.
_.meanBy(objects, 'n');
// => 5