Lodash _.sumBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Math

Definition

Like _.sum, except it accepts an iteratee invoked for each element to generate the value to sum. Part of Lodash Math (stable — not deprecated). The iteratee is called with one argument: (value).

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

Syntax

Call _.sumBy. Start with this line.

Lodash
_.sumBy(array, iteratee)
CallWhat it does
_.sumBy(array, iteratee)Primary signature.

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

Library declarations
Lodash
_.sumBy(array, iteratee)

Parameters

ParameterDescription
arrayRequired. Array to iterate over
iterateeOptional. Function or property path; invoked as (value)

Return Value

Return ValueDescription
*The sum (number), or 0 if empty/nullish

Lodash Examples

Example 1 — Official: Function Iteratee

Return the field you want to add.

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

console.log(_.sumBy(objects, function (o) {
  return o.n;
})); // 20
Try It Yourself

How It Works

  1. Input. Four objects with an n field.
  2. Iteratee. Function returns o.n for each item.
  3. Sum. 4 + 2 + 8 + 6 → 20.
  4. Docs. Matches the official Lodash example.

Example 2 — Official: Property Shorthand

Pass the field name as a string — same result.

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

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

How It Works

  1. Shorthand. 'n' is the _.property iteratee.
  2. Pull. Lodash reads o.n for each object.
  3. Sum. Same total: 20.
  4. When. Prefer this for a simple field path.

Example 3 — Custom Iteratee (Computed Values)

Transform each item before adding.

Lodash
var items = [
  { price: 10, qty: 2 },
  { price: 5, qty: 3 }
];

console.log(_.sumBy(items, function (o) {
  return o.price * o.qty;
})); // 35
Try It Yourself

How It Works

  1. Line totals. 10 * 2 and 5 * 3.
  2. Add. 20 + 15 → 35.
  3. Why function. Shorthand cannot multiply two fields.
  4. Use. Carts, scores, weighted sums.

Example 4 — Missing Fields Are Skipped

Undefined iteratee results do not become NaN.

Lodash
var rows = [{ price: 1 }, {}, { price: 2 }];

console.log(_.sumBy(rows, "price")); // 3
Try It Yourself

How It Works

  1. Present. 1 and 2 are added.
  2. Missing. {}.price is undefined → skipped.
  3. Result. 3, not NaN.
  4. Want zero? Use o => o.price ?? 0.

Example 5 — _.sum vs _.sumBy

Flat numbers use sum; object fields use sumBy.

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

console.log(_.sum(nums));        // 20
console.log(_.sumBy(objects, "n")); // 20
Try It Yourself

How It Works

  1. Flat. _.sum adds the numbers directly.
  2. Objects. _.sumBy pulls n first.
  3. Same total. Both paths yield 20.
  4. Rule. Numbers → sum; properties → sumBy.

Browser & Runtime Support

_.sumBy is a Lodash Math helper available in 4.x (and earlier builds). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native equivalents map then reduce; Lodash adds iteratee shorthands and skips undefined results.

✓ Stable · Lodash 4.x

_.sumBy()

Available in [email protected] full builds and the lodash/sumBy 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
_.sumBy Lodash 4.x

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import sumBy from "lodash/sumBy" for smaller builds. Use property shorthand for simple fields; use a function for computed values.

Frequently Asked Questions

_.sumBy(array, iteratee) is like _.sum, but each element is passed through an iteratee first. Official docs: sum objects by function or by property shorthand 'n' → 20.
_.sum adds raw array values. _.sumBy maps each element with an iteratee (function or property path) then adds those numbers. Without a useful iteratee on objects, prefer _.sum on a flat number list.
When the iteratee returns undefined (for example a missing field), that row is skipped. The total is the sum of rows that had a value — not NaN.
If any iteratee return is a string, native + switches to concatenation. Coerce inside the iteratee: _.sumBy(rows, r => Number(r.price)).
_.sumBy skips undefined iteratee results and has no divisor. _.meanBy still divides by array.length, so missing fields can lower the average.
Use import sumBy from "lodash/sumBy" or require("lodash/sumBy") so bundlers can tree-shake. Full build: import _ from "lodash" then _.sumBy.

Explore Math methods

Browse add, mean, max, min, and more Lodash math helpers.

Math methods hub →

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.

10 people found this page helpful