Files
30-seconds-of-code/snippets/findKeys.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

577 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
findKeys object,beginner 2020-10-25T09:59:13+02:00 2020-11-15T14:43:44+02:00

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

  • Use Object.keys(obj) 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' ]