-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
New function findKeys proposition #2676
Comments
I can't speak for the maintainers but it seems like a pretty specific use-case. And the fact that Lodash doesn't implement anything similar makes it even more suspect. I can see wanting a list of objects that match the predicate, but I've never needed to find keys matching a predicate on a single object. Can you give a real-world example of where you need to find keys matching a predicate on an object? All that said, it's pretty easy to implement yourself using Underscore methods: _.findKeys = function(object, predicate, context) {
var keyOrNil = function(value, key) {
if (predicate(value, key)) return key;
};
return _.compact(_.mapObject(object, keyOrNil, context));
}; |
Hey @FabriceGaudin. Can you talk a little bit about the specific use cases where this function would be come in handy? |
@wesvetter Lodash implements it : https://lodash.com/docs/4.17.4#findKey |
@jashkenas By @wesvetter's logic, that should be enough to implement it in underscore, no ? ^^ |
@sebastien-mignot You're mixing up findKey (singular) with |
@jdalton |
Nope, there are many functions that Lodash implements that Underscore does not.
Is it common enough that it needs to be added to the core library though? A silly/contrived counter-example of that logic would be making |
Thank you for all your responses, here is my use case. var race = {
"name": "Paris' marathon",
"progressByRunners": {
"Moe": 39.1,
"Larry": 23.3,
"Curly": 35.2
}
}; And I'd like to have all the runners whose progress is greater than 30. cc @jashkenas |
I like it! I'd be happy to merge a pull request that implements it nicely, with tests. |
@jashkenas Hi ! Here is my PR: #2681 |
I'd like to use a function but it seems it's missing. I didn't find any issue for this, and I didn't find it in https://github.com/documentcloud/underscore-contrib (but I'm not sure how to search efficiently here)
function description:
findKeys
_.findKeys(object, predicate, [context])
Returns an array of keys where the predicate truth test passes (or empty array).
_.findKeys({ a: 1, b: 2, c: 3, d: 2}, function (val, key) { return val === 2; })
=> ["b", "d"]
_.findKeys({ a: 1, b: 2, c: 3, d: 2}, function (val, key) { return val === 4; })
=> []
Am I the only one who would need this ? If not, i can do a PR
The text was updated successfully, but these errors were encountered: