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

sumBy returns bool instead of integer when the result equals 1 #2058

Closed
JonWallsten opened this issue Feb 26, 2016 · 9 comments
Closed

sumBy returns bool instead of integer when the result equals 1 #2058

JonWallsten opened this issue Feb 26, 2016 · 9 comments
Labels

Comments

@JonWallsten
Copy link

I've noticed that sumBy in Lodash 4 returns bool true instead of 1 if the sum is 1. If the sum is 2 or more it returns an integer as usual. Is this expected behavior?

@JonWallsten JonWallsten changed the title sumBy returns true when it's only one match sumBy returns bool instead of integer when the result equals 1 Feb 26, 2016
@jdalton
Copy link
Member

jdalton commented Feb 26, 2016

_.sumBy([{ 'x': 1 }], 'x')
// 1

@JonWallsten
Copy link
Author

//list.items
//[{isNew: true}]
list.newItemCount = _.sumBy(list.items, {isNew: true});
//true

//list.items
//[{isNew: true}, {isNew: true}]
//list.newItemCount = _.sumBy(list.items, {isNew: true});
//2

@jdalton
Copy link
Member

jdalton commented Feb 26, 2016

See. next time, lead with an example :P

You're using the iteratee a bit odd by using the matches shorthand as it will produce a boolean which is what will be used to compute the sum.

true + true // 2

@JonWallsten
Copy link
Author

That's a weird behavior for "sum". So how do I count the number of occurrences of isNew: true in one line? I mean, this code was working until I updated to Lodash 4.

Edit: _.sumBy(list.items, 'isNew'); returns the same thing. So the problem is that 'isNew' is a bool?

@JonWallsten
Copy link
Author

_.countBy(list.items, 'isNew')['true']; // seems a bit ugly?

@jdalton
Copy link
Member

jdalton commented Feb 26, 2016

So the problem is that 'isNew' is a bool?

Yeah, though you could do

_.sumBy(list.items, _.flow(_.property('isNew'), Number));

or

_.countBy(list.items, 'isNew')['true']; // seems a bit ugly?

Naw, it's great. I was just going to suggest it. It's also better suited for the given task.

@JonWallsten
Copy link
Author

Cool, I guess I have to live with that from now on.

@thadd
Copy link

thadd commented Nov 28, 2018

Why is this closed as invalid? This is really counter-intuitive behavior. I can't see any reason why sumBy should ever return anything except for a number.

@lock
Copy link

lock bot commented Nov 29, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants