Merge pull request #1697 from jun-low/jun/feat-find-keys

feat: add findKeys snippet
This commit is contained in:
Isabelle Viktoria Maciohsek
2020-11-15 14:44:20 +02:00
committed by GitHub

24
snippets/findKeys.md Normal file
View File

@ -0,0 +1,24 @@
---
title: findKeys
tags: object,beginner
---
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.
```js
const findKeys = (obj, val) =>
Object.keys(obj).filter(key => obj[key] === val);
```
```js
const ages = {
Leo: 20,
Zoey: 21,
Jane: 20,
};
findKeys(ages, 20); // [ 'Leo', 'Jane' ]
```