-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
44 lines (32 loc) · 808 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Searches throug every level of the json to look for a key
* @param {*} key
* @param {*} json
*/
function hasAttr(key, json) {
// If either key or json is null, return error
if (!json || !key || !isJson(json)){
return false;
}
// If the key is available at the first level
if (json.hasOwnProperty(key)){
return true;
}
// Iterate through all keys and pass every level to hasAttr recursively
var keys = Object.keys(json);
for (var i = 0; i < keys.length; i++) {
var objKey = keys[i];
if (hasAttr(key, json[objKey])) {
return true;
}
}
return false;
}
function isJson(input) {
/* eslint-disable valid-typeof */
if (typeof input !== 'object' && typeof input !== 'array') {
return false;
}
return true;
}
module.exports = hasAttr;