How to use the mean function from lodash

Find comprehensive JavaScript lodash.mean code examples handpicked from public code repositorys.

lodash.mean is a function in the Lodash library that calculates the arithmetic mean of a given array of numbers.

255
256
257
258
259
260
261
262
263
264
module.exports.mapcat              = _.mapcat;
module.exports.matches             = _.matches;
module.exports.matchesProperty     = _.matchesProperty;
module.exports.max                 = _.max;
module.exports.maxBy               = _.maxBy;
module.exports.mean                = _.mean;
module.exports.meanBy              = _.meanBy;
module.exports.memoize             = _.memoize;
module.exports.merge               = _.merge;
module.exports.mergeWith           = _.mergeWith;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

551
552
553
554
555
556
557
558
559
560
561
562
563
console.log(max); // => 8


const maxBy = _.maxBy([{ 'n': 1 }, { 'n': 2 }], o => o.n);
console.log(maxBy); // => { 'n': 2 }


const mean = _.mean([4, 2, 8, 6]);
console.log(mean); // => 5


const meanBy = _.meanBy([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], o => o.n);
console.log(meanBy); // => 5
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.mean work?

lodash.mean is a function in the Lodash library that takes an array of numbers as input and calculates the arithmetic mean of those numbers.

The function first checks if the input array is empty, and if so, it returns NaN. If the input array is not empty, the function calculates the sum of all the numbers in the array using the lodash.sum function.

Once the sum is calculated, the function divides the sum by the length of the input array to calculate the arithmetic mean. The mean is then returned as the output of the function.

The lodash.mean function can be used to calculate the average value of a set of numbers, which can be useful in a variety of applications, such as statistics, finance, and data analysis.

41
42
43
44
45
46
47
48
49
50
const selectedStepNodes = nearbyAndSelected.nodes().filter('.step');
const nodeGroups =
    _.groupBy(selectedStepNodes, (node) => node.data('parent'));
const means = _.mapValues(
    nodeGroups,
    (nodes) => _.mean(_.map(nodes, node => node.position()[stepDim])));
_.each(cy.$('node.component'), (compNode) => {
  if (means[compNode.id()] === undefined) {
    return;
  }
fork icon0
star icon0
watch icon1

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
const _ = require("lodash");

const numbers = [2, 4, 6, 8, 10];

const mean = _.mean(numbers);

console.log(mean); // Output: 6

In this example, we first import the lodash library and define an array of numbers. We then use the lodash.mean function to calculate the arithmetic mean of the numbers in the array. The function returns 6 because it is the average of the numbers in the array. Finally, we print the arithmetic mean to the console using console.log.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)