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

  • Find first matching key

    JavaScript, Object

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

  • Find last matching key

    JavaScript, Object

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

  • Rename object keys

    JavaScript, Object

    Replaces the names of multiple object keys with the values provided.