diff --git a/README.md b/README.md index fc9d10648..ba96ed086 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,8 @@ average(1, 2, 3); * [`defaults`](#defaults) * [`equals`](#equals-) +* [`findKey`](#findkey) +* [`findLastKey`](#findlastkey) * [`forOwn`](#forown) * [`forOwnRight`](#forownright) * [`functions`](#functions) @@ -4240,6 +4242,67 @@ equals({ a: [2, { e: 3 }], b: [4], c: 'foo' }, { a: [2, { e: 3 }], b: [4], c: 'f
[⬆ Back to top](#table-of-contents) +### findKey + +Returns the first key that satisfies the provided testing function. Otherwise `undefined` is returned. + +Use `Object.keys(obj)` to get all the properties of the object, `Array.find()` to test the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object. + +```js +const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj)); +``` + +
+Examples + +```js +findKey( + { + barney: { age: 36, active: true }, + fred: { age: 40, active: false }, + pebbles: { age: 1, active: true } + }, + o => o['active'] +); // 'barney' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### findLastKey + +Returns the last key that satisfies the provided testing function. Otherwise `undefined` is returned. + +Use `Object.keys(obj)` to get all the properties of the object, `Array.reverse()` to reverse their order and `Array.find()` to test the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object. + +```js +const findLastKey = (obj, fn) => + Object.keys(obj) + .reverse() + .find(key => fn(obj[key], key, obj)); +``` + +
+Examples + +```js +findLastKey( + { + barney: { age: 36, active: true }, + fred: { age: 40, active: false }, + pebbles: { age: 1, active: true } + }, + o => o['active'] +); // 'pebbles' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### forOwn Iterates over all own properties of an object, running a callback for each one. diff --git a/docs/index.html b/docs/index.html index 90e378a0e..35079591d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -949,6 +949,27 @@ console.log<
   return keys.every(k => equals(a[k], b[k]));
 };
 
equals({ a: [2, { e: 3 }], b: [4], c: 'foo' }, { a: [2, { e: 3 }], b: [4], c: 'foo' }); // true
+

findKey

Returns the first key that satisfies the provided testing function. Otherwise undefined is returned.

Use Object.keys(obj) to get all the properties of the object, Array.find() to test the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.

const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj));
+
findKey(
+  {
+    barney: { age: 36, active: true },
+    fred: { age: 40, active: false },
+    pebbles: { age: 1, active: true }
+  },
+  o => o['active']
+); // 'barney'
+

findLastKey

Returns the last key that satisfies the provided testing function. Otherwise undefined is returned.

Use Object.keys(obj) to get all the properties of the object, Array.reverse() to reverse their order and Array.find() to test the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.

const findLastKey = (obj, fn) =>
+  Object.keys(obj)
+    .reverse()
+    .find(key => fn(obj[key], key, obj));
+
findLastKey(
+  {
+    barney: { age: 36, active: true },
+    fred: { age: 40, active: false },
+    pebbles: { age: 1, active: true }
+  },
+  o => o['active']
+); // 'pebbles'
 

forOwn

Iterates over all own properties of an object, running a callback for each one.

Use Object.keys(obj) to get all the properties of the object, Array.forEach() to run the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.

const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key, obj));
 
forOwn({ foo: 'bar', a: 1 }, v => console.log(v)); // 'bar', 1
 

forOwnRight

Iterates over all own properties of an object in reverse, running a callback for each one.

Use Object.keys(obj) to get all the properties of the object, Array.reverse() to reverse their order and Array.forEach() to run the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.

const forOwnRight = (obj, fn) =>
diff --git a/snippets/findKey.md b/snippets/findKey.md
index 96d563f83..81e41d226 100644
--- a/snippets/findKey.md
+++ b/snippets/findKey.md
@@ -5,10 +5,16 @@ Returns the first key that satisfies the provided testing function. Otherwise `u
 Use `Object.keys(obj)` to get all the properties of the object, `Array.find()` to test the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.
 
 ```js
-const findKey = (obj, fn) =>
-  Object.keys(obj).find(key => fn(obj[key], key, obj));
+const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj));
 ```
 
 ```js
-findKey({barney: { age: 36, active: true }, fred: { age: 40, active: false }, pebbles: { age: 1, active: true }}, o => o['active']); // 'barney'
+findKey(
+  {
+    barney: { age: 36, active: true },
+    fred: { age: 40, active: false },
+    pebbles: { age: 1, active: true }
+  },
+  o => o['active']
+); // 'barney'
 ```
diff --git a/snippets/findLastKey.md b/snippets/findLastKey.md
index ee1390a21..1333a98d3 100644
--- a/snippets/findLastKey.md
+++ b/snippets/findLastKey.md
@@ -6,9 +6,18 @@ Use `Object.keys(obj)` to get all the properties of the object, `Array.reverse()
 
 ```js
 const findLastKey = (obj, fn) =>
-  Object.keys(obj).reverse().find(key => fn(obj[key], key, obj));
+  Object.keys(obj)
+    .reverse()
+    .find(key => fn(obj[key], key, obj));
 ```
 
 ```js
-findLastKey({barney: { age: 36, active: true }, fred: { age: 40, active: false }, pebbles: { age: 1, active: true }}, o => o['active']); // 'pebbles'
+findLastKey(
+  {
+    barney: { age: 36, active: true },
+    fred: { age: 40, active: false },
+    pebbles: { age: 1, active: true }
+  },
+  o => o['active']
+); // 'pebbles'
 ```