8000 sumBy returns boolean instead of number when summing booleans · Issue #5985 · lodash/lodash · GitHub
[go: up one dir, main page]

Skip to content

sumBy returns boolean instead of number when summing booleans #5985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
johnmasonyxe opened this issue Apr 25, 2025 · 0 comments
Open

sumBy returns boolean instead of number when summing booleans #5985

johnmasonyxe opened this issue Apr 25, 2025 · 0 comments

Comments

@johnmasonyxe
Copy link
johnmasonyxe commented Apr 25, 2025

description:

when using _.sumBy on an array of objects with boolean values, the result is a boolean when the array contains only one item

reproduction

run this code on https://playcode.io/lodash:

import _ from 'lodash';

const result = _.sumBy([{ bool: true }], 'bool');

console.log(_.VERSION);         // 4.17.21
console.log(result);            // expected: 1, actual: true
console.log(typeof result);     // expected: 'number', actual: 'boolean
console.log(result === true);   // expected: false, actual: true
console.log(result === 1);      // expected: true, actual: false

expected behaviour
lodash should coerce boolean values to numbers (true → 1, false → 0) consistently, even for single-item arrays.

notes
for quick fix replace

_.sumBy([{ bool: true }], 'bool')

with

_.filter([{ bool: true }], item => item.bool).length

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant
0