Travis build: 76
This commit is contained in:
40
README.md
40
README.md
@ -355,6 +355,7 @@ average(1, 2, 3);
|
||||
* [`bindAll`](#bindall)
|
||||
* [`deepClone`](#deepclone)
|
||||
* [`defaults`](#defaults)
|
||||
* [`dig`](#dig)
|
||||
* [`equals`](#equals-)
|
||||
* [`findKey`](#findkey)
|
||||
* [`findLastKey`](#findlastkey)
|
||||
@ -6369,6 +6370,45 @@ defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 }
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### dig
|
||||
|
||||
Returns the target value in a nested JSON object, based on the given key.
|
||||
|
||||
Use the `in` operator to check if `target` exists in `obj`.
|
||||
If found, return the value of `obj[target]`, otherwise use `Object.values(obj)` and `Array.reduce()` to recursively call `dig` on each nested object until the first matching key/value pair is found.
|
||||
|
||||
```
|
||||
const dig = (obj, target) =>
|
||||
target in obj
|
||||
? obj[target]
|
||||
: Object
|
||||
.values(obj)
|
||||
.reduce((acc, val) => {
|
||||
if (acc !== undefined) return acc;
|
||||
if (typeof val === 'object') return dig(val, target);
|
||||
}, undefined);
|
||||
```
|
||||
|
||||
```
|
||||
const data = {
|
||||
level1:{
|
||||
level2:{
|
||||
level3: 'some data'
|
||||
}
|
||||
}
|
||||
};
|
||||
dig(data, 'level3'); // 'some data'
|
||||
dig(data, 'level4'); // undefined
|
||||
```<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### equals 
|
||||
|
||||
Performs a deep comparison between two values to determine if they are equivalent.
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user