Skip to content
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

Method sumBy returns string #5818

Open
ekabolotina opened this issue Feb 13, 2024 · 4 comments
Open

Method sumBy returns string #5818

ekabolotina opened this issue Feb 13, 2024 · 4 comments

Comments

@ekabolotina
Copy link

ekabolotina commented Feb 13, 2024

There are cases when method sumBy could return string instead of number.

Current behavior:

_.sumBy([{ a: 1 }, { a: 'f' }, { a: null }], 'a'); // '1fnull'

Returned value is of type string.

Expected behavior:

_.sumBy([{ a: 1 }, { a: 'f' }, { a: null }], 'a'); // NaN

Returned value is of type number.

lpabinash pushed a commit to lpabinash/lodash that referenced this issue Feb 14, 2024
@katsanva
Copy link

katsanva commented Feb 15, 2024

Lets see how JS behaves in this case:

> 1 + 'f' + null
"1fnull"

So Lodash is doing things right!

If you actually do want to have NaN for some reason, you should use Number(_.sumBy([{ a: 1 }, { a: 'f' }, { a: null }], 'a')) which equals to Number("1fnull") and evaluates to NaN

@AmanAmbasta
Copy link

Well according to the function definition, it should return number
image

@arnavpanwar99
Copy link

@AmanAmbasta NaN is also a number, so according to function definition its correct as well.

@Trott
Copy link

Trott commented May 9, 2024

@AmanAmbasta NaN is also a number, so according to function definition its correct as well.

It's not returning NaN. It's returning a string. That's the whole point of this bug report.

_.sumBy([{ a: 1 }, { a: 'f' }, { a: null }], 'a'); // '1fnull'

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

5 participants