E_Jovi E_Jovi - 1 year ago 127
Javascript Question

algorithm of findKey in lodash

I want to find key via value from object, and I find that lodash provide us convenient api

could help me.

but my question is, how could
work? is that different from hash common key value mapping?

I don't know anything about that, if possible, could you provide me some example to compare?

thanks for your time!

Answer Source

Lodash is an open-source library, you can inspect its source to check their implementations of all functions.


findKey can probably be traced down to line 716's baseFindKey function:

function baseFindKey(collection, predicate, eachFunc) {
  var result;
  eachFunc(collection, function(value, key, collection) {
    if (predicate(value, key, collection)) {
      result = key;
      return false;
  return result;

What it does is it runs through the collection, and for every member inside the collection, run the predicate check to see if the member matches the predicate.

If it matches, the key would be returned as the result.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download