Travis build: 1511

This commit is contained in:
30secondsofcode
2019-10-16 06:39:13 +00:00
parent 4b6bce7ac8
commit ca2c1a51c0
14 changed files with 207 additions and 117 deletions

View File

@ -10,21 +10,20 @@ Use `typeof` to check if the contents of `obj[key]` are an `object` and, if so,
Otherwise, use `Object.keys(obj)` in combination with `Array.prototype.includes()` to check if the given `key` exists.
```js
const hasKey = (obj, key) => {
if (key.includes('.')) {
let _key = key.split('.')[0];
if (typeof obj[_key] === 'object')
return hasKey(obj[_key], key.slice(key.indexOf('.') + 1));
if (typeof obj[_key] === 'object') return hasKey(obj[_key], key.slice(key.indexOf('.') + 1));
}
return Object.keys(obj).includes(key);
};
```
```js
let obj = {
a: 1, b: { c: 4 }, 'd.e': 5
a: 1,
b: { c: 4 },
'd.e': 5
};
hasKey(obj, 'a'); // true
hasKey(obj, 'b'); // true