Update findKeys.md

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-11-15 14:43:44 +02:00
committed by GitHub
parent e58390f740
commit 34de86b53f

View File

@ -3,23 +3,22 @@ title: findKeys
tags: object,beginner tags: object,beginner
--- ---
Return all the keys in the provided object that match the given value. Finds all the keys in the provided object that match the given value.
- Use `Object.keys(object)` to get all the properties of the object. - 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 equal to the given value. - Use `Array.prototype.filter()` to test each key-value pair and return all keys that are equal to the given value.
```js ```js
const findKeys = (object, value) => const findKeys = (obj, val) =>
Object.keys(object).filter(key => object[key] === value); Object.keys(obj).filter(key => obj[key] === val);
``` ```
```js ```js
const ages = { const ages = {
"Leo" : 20, Leo: 20,
"Zoey" : 21, Zoey: 21,
"Jane" : 20 Jane: 20,
}; };
findKeys(ages, 20); // [ 'Leo', 'Jane' ]
findKeys(ages, 20); // [ "Leo", "Jane" ]
``` ```