Comments (15)
lodash will only handle arrays and does not handle objects in its implementation of max.
That is by design.
Lo-Dash is optimized for the common case. I moved _.max
and _.min
to the Arrays
category (and others as well) because all documentation and unit tests test array usage.
cc\ @kitcambridge @mathiasbynens opinions?
from lodash.
This breaks the "drop-in replacement for Underscore.js" tagline, which I feel has been a major part of lodash.
As with issue #16, I ran into this issue in my code by dropping in lodash as a replacement for Underscore.js and later getting bug reports.
from lodash.
You are using Underscore in an undocumented and untested way. I will not support this at this time but am open to revisiting it if more have this issue.
Until then you can use _.max(_.values(obj));
from lodash.
The iterator is entirely optional, per the Underscore documentation.
http://documentcloud.github.com/underscore/#max
"Returns the maximum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the value is ranked."
You are correct that this is untested, as per https://github.com/documentcloud/underscore/blob/master/test/collections.js#L203, but this seems like an oversight since this scenario is explicitly supported in the max implementation, https://github.com/documentcloud/underscore/blob/master/underscore.js#L225.
I defer to to your choice of not supporting this at this time.
from lodash.
The iterator is entirely optional, per the Underscore documentation.
http://documentcloud.github.com/underscore/#max
"Returns the maximum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the value is ranked."
Yap, Lo-Dash follows that too, the iterator (aka callback
) is optional
https://github.com/bestiejs/lodash/blob/master/lodash.js#L1317-1367
You are correct that this is untested, as per https://github.com/documentcloud/underscore/blob/master/test/collections.js#L203, but this seems like an oversight since this scenario is explicitly supported in the max implementation, https://github.com/documentcloud/underscore/blob/master/underscore.js#L225.
It's a side effect of Underscore's design limitations. If it wants native Array#forEach
it has to use _.each
which also happens to be crammed with object iteration sugar. For many methods it doesn't make sense to support objects and they just happen to slip by because of the overuse/dependency on _.each
.
from lodash.
Another example of Underscore's mixup/confusion is its _.shuffle
method which is stated to shuffle an array, but also internally uses _.each
and so indirectly supports objects.
from lodash.
Computing the maximum and minimum values for an object is a bit of an edge case, but it makes sense—unlike, say, shuffling an object's properties.
from lodash.
values for an object is a bit of an edge case
Yap, in this case I don't think support was intended and is just a side effect of Underscore's overloaded API which we are moving away from with the introduction of _.forIn and _.forOwn.
from lodash.
Mixup/confusion, side effect of design limitations or intentional choice - they do at least group all of these in their "Collections (Array or Objects)" section.
from lodash.
Mixup/confusion, side effect of design limitations or intentional choice - they do at least group all of these in their "Collections (Array or Objects)" section.
Underscore's design problems aren't limited to its API or implementation and also extend to its organization of methods. I disagree with several of the method category assignments and have adjusted them to better reflect real-world/common usage.
See also Underscore's invoke (under "Collections") vs. Lo-Dash's invoke (under "Arrays").
from lodash.
@thorlarholm Also, I just wanted to let you know that I am totally thankful for your feedback and previous feedback/bug reports. Because Lo-Dash doesn't have many of the restrictions of Underscore it gives me the opportunity to rethink some of the design (method categories, api, edge cases, etc.). I'm trying to strike a balance between Underscore compatibility and optimizations/clarifications for common usage.
Please let me know if _.max(_.values(obj));
will work for you and of any other issues you have in the future.
from lodash.
@jdalton I'm happy to give feedback, no hurt feelings here for not getting this changed; expect more feedback.
The alternate implementation is just as fine for me, and a bit more explicit at that.
from lodash.
As of Lo-Dash v0.8.2 _.max
, _.min
, and _.shuffle
work with objects.
from lodash.
Nice, thank you for the diligent work.
from lodash.
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.
from lodash.
Related Issues (20)
- Feature Request: multiple variables in _.template()
- _.defaultsDeeps return and mutate behaviour is inconsistent with null object
- Description of `compact` is incomplete.
- [5 years old bug] _.debounce().flush() fix never made it into production
- Sonatype shows a new critical security violation. HOT 3
- Would It Be Possible to Update the Typing of `max([0, anyNumber])` Such That It Returns Number? HOT 3
- Command injection vuln HOT 3
- Add "isWeakRef" function.
- sortBy nor orderBy not working properly
- Cannot import lodash single function in Node HOT 3
- keyBy not support index HOT 2
- lodash.groupby npm package version is still 4.6.0
- Strange _.rearg / _.reduce behaviour
- isEqual returns incorrect result
- bun build commands not working as expected
- orderBy orders field boolean dosent work HOT 1
- Please republish lodash.forin HOT 4
- Wrong version in lodash.pick vulnerability report CVE-2020-8203 HOT 8
- `lodash.merge` drops keys that are symbols
- Type of parameter object is string in function baseGet(object, path)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lodash.