underscore
underscore copied to clipboard
New function findKeys proposition
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
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 findKeys (plural)
@jdalton Ok, my bad. And if every one agrees that sometime you need to find one such key, isn't it reasonable to expect that sometime one will need to find all such keys ?
By @wesvetter's logic, that should be enough to implement it in underscore, no ?
Nope, there are many functions that Lodash implements that Underscore does not.
And if every one agrees that sometime you need to find one such key, isn't it reasonable to expect that sometime one will need to find all such keys?
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 _.kebabcaseObject just because Lodash supports _.kebabcase.
Thank you for all your responses, here is my use case. My object looks like this:
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. I know that I can do it myself by combining other underscore functions but I am wondering if it is useful for anyone else.
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: https://github.com/jashkenas/underscore/pull/2681 Tell me if I need to change anything