Files
30-seconds-of-code/snippets/js/s/find-matching-keys.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

595 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Find matching keys snippet javascript
object
beach-riders 2020-11-15T14:43:44+02:00

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

  • Use Object.keys() to get all the properties of the object.
  • Use Array.prototype.filter() to test each key-value pair and return all keys that are equal to 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' ]