From e58390f740b1ee47a8a19a0c05e9a9f1b9d0da2c Mon Sep 17 00:00:00 2001 From: jun-low Date: Sun, 25 Oct 2020 15:59:13 +0800 Subject: [PATCH 1/2] Create findKeys.md --- snippets/findKeys.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 snippets/findKeys.md diff --git a/snippets/findKeys.md b/snippets/findKeys.md new file mode 100644 index 000000000..b59a55f0d --- /dev/null +++ b/snippets/findKeys.md @@ -0,0 +1,25 @@ +--- +title: findKeys +tags: object,beginner +--- + +Return 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 `Array.prototype.filter()` to test each key-value pair and return all keys that equal to the given value. + + +```js +const findKeys = (object, value) => + Object.keys(object).filter(key => object[key] === value); +``` + +```js +const ages = { + "Leo" : 20, + "Zoey" : 21, + "Jane" : 20 +}; + +findKeys(ages, 20); // [ "Leo", "Jane" ] +``` From 34de86b53f74a39822d02abe71fb2274f2fea62f Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sun, 15 Nov 2020 14:43:44 +0200 Subject: [PATCH 2/2] Update findKeys.md --- snippets/findKeys.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/snippets/findKeys.md b/snippets/findKeys.md index b59a55f0d..e6b4030e3 100644 --- a/snippets/findKeys.md +++ b/snippets/findKeys.md @@ -3,23 +3,22 @@ title: findKeys 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 `Array.prototype.filter()` to test each key-value pair and return all keys that equal to 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 = (object, value) => - Object.keys(object).filter(key => object[key] === value); +const findKeys = (obj, val) => + Object.keys(obj).filter(key => obj[key] === val); ``` ```js const ages = { - "Leo" : 20, - "Zoey" : 21, - "Jane" : 20 + Leo: 20, + Zoey: 21, + Jane: 20, }; - -findKeys(ages, 20); // [ "Leo", "Jane" ] +findKeys(ages, 20); // [ 'Leo', 'Jane' ] ```