1-liners
Version:
Useful oneliners and shorthand functions
27 lines (24 loc) • 549 B
JavaScript
/**
* @module 1-liners/findKey
*
* @description
*
* A pure function to find key from object, matching a predicate
* similar to https://lodash.com/docs/4.17.4#findKey or Array.findIndex()
*
* @example
*
* const findKey = require('1-liners/findKey');
*
* const data = { a: 1, b: 2, c: 3 };
* findKey((x) => x > 2, data); // => 'c'
*
*/
;
exports.__esModule = true;
exports["default"] = function (fn, obj) {
return Object.keys(obj).find(function (k) {
return fn(obj[k]);
});
};
module.exports = exports["default"];