Created
February 25, 2026 19:12
-
-
Save petergi/b959eac6389eaf811786e3d73dd6fc20 to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment