underscore icon indicating copy to clipboard operation
underscore copied to clipboard

New function findKeys proposition

Open FabriceGaudin opened this issue 8 years ago • 10 comments

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

FabriceGaudin avatar Jun 06 '17 16:06 FabriceGaudin

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

wesvetter avatar Jun 12 '17 17:06 wesvetter

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

jashkenas avatar Jun 12 '17 19:06 jashkenas

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

sebastien-mignot avatar Jun 13 '17 08:06 sebastien-mignot

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

sebastien-mignot avatar Jun 13 '17 08:06 sebastien-mignot

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

jdalton avatar Jun 13 '17 08:06 jdalton

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

sebastien-mignot avatar Jun 13 '17 08:06 sebastien-mignot

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.

wesvetter avatar Jun 13 '17 19:06 wesvetter

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

FabriceGaudin avatar Jun 14 '17 10:06 FabriceGaudin

I like it!

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

jashkenas avatar Jun 14 '17 15:06 jashkenas

@jashkenas Hi ! Here is my PR: https://github.com/jashkenas/underscore/pull/2681 Tell me if I need to change anything

FabriceGaudin avatar Jul 06 '17 16:07 FabriceGaudin