Travis build: 76

This commit is contained in:
30secondsofcode
2018-07-14 07:34:58 +00:00
parent 5da8374029
commit bcced2bed7
12 changed files with 70 additions and 12 deletions

View File

@ -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 ![advanced](/advanced.svg)
Performs a deep comparison between two values to determine if they are equivalent.