Travis build: 1340
This commit is contained in:
@ -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'
|
||||
```
|
||||
|
||||
@ -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'
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user