Lodash _.meanBy() Method
Last Updated :
03 Nov, 2023
Lodash _.meanBy() method is used to compute the mean value from the original array by iterating over each element in the array using the Iteratee function. It is almost the same as the _.mean() function.
Syntax:
_.meanBy(array, [iteratee = _.identity]);
Parameters:
- array: It is the array that the method iterates over to get the mean of all the elements.
- iteratee: It is the function that is invoked for every element in the array.
Return Value:
This method returns the mean value of the array.
Example 1: In this example, we are getting the mean of the value of the array of objects with the help of the lodash _.meanBy() method.
Javascript
const _ = require( "lodash" );
let arr = [{ 'n' : 4 }, { 'n' : 2 }, { 'n' : 6 }];
let mean_val =
_.meanBy(arr, function (o) { return o.n; });
console.log(mean_val);
|
Output:
4
Example 2: In this example, we are getting the mean of the value of the array of objects with the help of the lodash _.meanBy() method.
Javascript
const _ = require( "lodash" );
let arr = [{ 'n' : 10 }, { 'n' : 5 },
{ 'n' : 3 }, { 'n' : 12 }];
let mean_val = _.meanBy(arr, 'n' );
console.log(mean_val);
|
Output:
7.5
Share your thoughts in the comments
Please Login to comment...