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

New function findKeys proposition #2676

Open
FabriceGaudin opened this issue Jun 6, 2017 · 10 comments
Open

New function findKeys proposition #2676

FabriceGaudin opened this issue Jun 6, 2017 · 10 comments
Labels
contrib probably belongs in Underscore-contrib enhancement

Comments

@FabriceGaudin
Copy link

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

@wesvetter
Copy link

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));
};

@jashkenas
Copy link
Owner

jashkenas commented Jun 12, 2017

Hey @FabriceGaudin. Can you talk a little bit about the specific use cases where this function would be come in handy?

@sebastien-mignot
Copy link

@wesvetter Lodash implements it : https://lodash.com/docs/4.17.4#findKey

@sebastien-mignot
Copy link

@jashkenas By @wesvetter's logic, that should be enough to implement it in underscore, no ? ^^

@jdalton
Copy link
Contributor

jdalton commented Jun 13, 2017

@sebastien-mignot You're mixing up findKey (singular) with findKeys (plural)

@sebastien-mignot
Copy link

@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 ?

@wesvetter
Copy link

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.

@FabriceGaudin
Copy link
Author

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

@jashkenas
Copy link
Owner

I like it!

I'd be happy to merge a pull request that implements it nicely, with tests.

@FabriceGaudin
Copy link
Author

@jashkenas Hi ! Here is my PR: #2681
Tell me if I need to change anything

@jgonggrijp jgonggrijp added the contrib probably belongs in Underscore-contrib label Aug 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contrib probably belongs in Underscore-contrib enhancement
Projects
None yet
Development

No branches or pull requests

6 participants