Lodash _.mean() 方法
Lodash是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。
_.mean()方法用于查找数组中值的平均值。
句法:
_.mean( array )
参数:此方法接受如上所述和如下所述的单个参数:
- 数组:它是该方法迭代以获得所有值的平均值的数组。
返回值:此方法返回数组中值的平均值。
示例 1:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.mean()
// method
let mean_val = _.mean([15, 7, 38, 46, 82]);
// Printing the output
console.log(mean_val);
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.mean()
// method
let mean_val = _.mean([2, 3, 4, 6, 7, 8]);
// Printing the output
console.log(mean_val);
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.mean()
// method
let mean_val = _.mean([10, -4, -6, 8, 12]);
// Printing the output
console.log(mean_val);
输出:
37.6
示例 2:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.mean()
// method
let mean_val = _.mean([2, 3, 4, 6, 7, 8]);
// Printing the output
console.log(mean_val);
输出:
5
示例 3:
Javascript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.mean()
// method
let mean_val = _.mean([10, -4, -6, 8, 12]);
// Printing the output
console.log(mean_val);
输出:
4