Lodash _.meanBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Math

Definition

Like _.mean, except each element is passed through an iteratee first — then those results are averaged. Part of Lodash 4.x Math (since 4.7.0).

Pass the numbers or collection shown below. Check the return value before you chain another math helper.

Syntax

Call _.meanBy. Start with this line.

Lodash
_.meanBy(array, [iteratee=_.identity])
CallWhat it does
_.meanBy(array, [iteratee=_.identity])Primary signature.
_.meanBy(array, iteratee)Run the math helper on numbers or a collection.

Prefer import meanBy from "lodash/meanBy" when you want a smaller bundle.

Library declarations
Lodash
_.meanBy(array, [iteratee=_.identity])

Parameters

ParameterDescription
arrayRequired. Collection to iterate
iterateeOptional. Function or property path; default identity

Return Value

Return ValueDescription
*Mean of projected values, or NaN if empty

Lodash Examples

Example 1 — Official Function Iteratee

Average n with an explicit callback.

Lodash
var objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }];

_.meanBy(objects, function (o) {
  return o.n;
});
// → 5
Try It Yourself

How It Works

  1. Map. Iteratee returns each object’s n.
  2. Values. Projected list is [4, 2, 8, 6].
  3. Average. 20 / 4 = 5.
  4. Arity. Iteratee is called with one argument: (value).

Example 2 — Property Shorthand

Same result with the "n" path.

Lodash
var objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }];

_.meanBy(objects, "n");
// → 5
Try It Yourself

How It Works

  1. Shorthand. "n" acts like o => o.n.
  2. Same math. Still averages 4, 2, 8, 6.
  3. Nested. Paths like "score.value" also work.
  4. Prefer. Use paths for simple fields; functions for custom math.

Example 3 — Empty Array Returns NaN

Same empty rule as _.mean.

Lodash
_.meanBy([], "n");
// → NaN

_.meanBy(null, "n");
// → NaN

Number.isFinite(_.meanBy([], "n"));
// → false
Try It Yourself

How It Works

  1. Empty. Nothing to project → 0 / 0.
  2. Nullish. Same NaN fallback.
  3. Guard. Check with Number.isFinite before display.
  4. Note. Not 0 and not undefined.

Example 4 — Missing Field Drag

Undefined projections still inflate the divisor.

Lodash
var rows = [{ n: 10 }, { n: 20 }, {}];

_.meanBy(rows, "n");
// → 10          // (10 + 20) / 3

_.meanBy([{ n: 10 }, { n: 20 }], "n");
// → 15
Try It Yourself

How It Works

  1. Missing. {} projects to undefined.
  2. Sum. Only 10 + 20 contribute.
  3. Divide. Still ÷ 3 (full array length).
  4. Fix. Filter incomplete rows before averaging.

Example 5 — vs _.mean and _.sumBy

Pick average vs total, raw vs projected.

Lodash
var objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }];
var nums = [4, 2, 8, 6];

_.meanBy(objects, "n");
// → 5

_.mean(nums);
// → 5

_.sumBy(objects, "n");
// → 20
Try It Yourself

How It Works

  1. meanBy. Average a field on objects.
  2. mean. Average a plain number array.
  3. sumBy. Total projected values (no divide).
  4. Choose. Objects + average → meanBy; total → sumBy.

Browser & Runtime Support

_.meanBy is a Lodash 4.x Math helper (since 4.7.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native cousin: map + reduce / length.

✓ Stable · Lodash 4.7.0+

_.meanBy()

Available in [email protected] full builds and the lodash/meanBy modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.meanBy Lodash 4.7+

Bottom line: Safe for production once Lodash is loaded. Prefer import meanBy from "lodash/meanBy" for smaller builds. Ideal for averaging object fields.

Frequently Asked Questions

Like _.mean, but each element is projected by an iteratee first. Official: meanBy([{n:4},{n:2},{n:8},{n:6}], 'n') → 5.
A function (value) => number, or a property-path shorthand like 'n' / 'score.value'.
NaN — same empty behavior as _.mean.
mean averages the elements themselves. meanBy averages the iteratee results.
undefined projections are skipped from the sum but still counted in array.length, which pulls the average down.
Use sumBy when you need the total of projected values, not the average.

Next: _.min()

Find the smallest value in an array of numbers.

_.min() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful