-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject-util.js
More file actions
36 lines (35 loc) · 831 Bytes
/
object-util.js
File metadata and controls
36 lines (35 loc) · 831 Bytes
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
exports.extractObjectKeyPath = function(obj, key) {
var indexList = {};
var level = 0;
var path = [];
var iterObject = function(obj, key) {
var myKeys = Object.keys(obj);
for (var i = 0, k; k = myKeys[i]; i++) {
var v = obj[k];
if (typeof v === 'object') {
path[level] = k;
level++;
iterObject(v, key);
} else if (typeof v === 'string') {
if (k === key) {
var currentPath = path.slice(0, level);
indexList[v] = {
path: currentPath.join('.'),
type: obj.type
};
}
}
}
level--;
};
iterObject(obj, key);
return indexList;
};
exports.getValueByPath = function(obj, path) {
var pathList = path.split('.');
var k;
while(k = pathList.shift()) {
obj = obj[k];
}
return obj;
};