Find matching keys

JavaScript, Object · Nov 15, 2020

Finds all the keys in the provided object that match the given value.

const findKeys = (obj, val) =>
  Object.keys(obj).filter(key => obj[key] === val);

const ages = {
  Leo: 20,
  Zoey: 21,
  Jane: 20,
};
findKeys(ages, 20); // [ 'Leo', 'Jane' ]

More like this

  • Omit matching object keys

    Creates an object composed of the properties the given function returns falsy for.

    JavaScript, Object · Oct 21, 2020

  • Pick matching object keys

    Creates an object composed of the properties the given function returns truthy for.

    JavaScript, Object · Oct 22, 2020

  • Find first matching key

    Finds the first key that satisfies the provided testing function. Otherwise undefined is returned.

    JavaScript, Object · Oct 22, 2020