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)
- Merge mutates source object if nested property is a class instance
- Future compatibility with Node.js/npm HOT 1
- Allow Default Value for toNumber("problem", 0) HOT 8
- isBuffer from lodash-es doesn't work HOT 1
- curryRight is broken in list.map context HOT 1
- Avoiding Lodash version disclosure HOT 1
- multiple vulnerabilities that exist in lodash.findlast HOT 4
- Attempt structuredClone in cloneDeep before doing manual clone HOT 6
- _.trimStart not work with "/" char
- _.kebabCase and _.camelCase treat some digits as delimiters but not others HOT 1
- async attempt
- lodash-es outdated HOT 1
- feature request: Safe maths functions HOT 6
- Alias `_.tail` to `_.last` HOT 2
- _.deburr() - ț not convert to t HOT 3
- Typescript error HOT 7
- Discussion on Performance Issues of `trimStart` and `trimEnd`(关于 `trimStart` 和 `trimEnd` 性能问题的讨论) HOT 3
- array _.difference bug HOT 1
- CVE-2021-23337 lodash versions prior to 4.17.21 are vulnerable to Command Injection via the template function. HOT 4
- There may be a bug here HOT 2
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.