-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindPath.js
62 lines (54 loc) · 1.69 KB
/
FindPath.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var items =
{"itemList": {"items": [
{"id": "item1"},
{"id": "item2","label": "Item 2"},
{"id": "item3"},
{"id": "item4"},
{"id": "item5"},
{"id": "subItem1",
"subItems": [
{"id": "subItem1Item1","label": "SubItem 1"},
{"id": "subItem1Item2","label": "SubItem 2"},
{"id": "subItem1Item3","label": "SubItem 3"}
]
},
{"id": "item6"},
{"id": "item7","label": "Item 7"},
{"id": "subItem2",
"subItems": {"id": "item1","label": "SubItem 2 item1"}
}
]}}
/** Check if the string is a number or not.
* Returns true if it's a number, false otherwise */
function isNumeric(num){
return !isNaN(num);
}
/** Returns the full path given a value.
* @param: data is the JSON file
value is the value that we need to find
path is the previous path */
function findPath(data, value, path)
{
path = path || "";
var finalpath = [];
// Check if data is valid
if(typeof(data) != "object" || Object.keys(data).length == 0)
return ( "input is not the correct type or object is empty" );
for(var obj in data) {
if (data[obj] == value)
return ( path + "/" + obj);
else if (typeof data[obj] == "object") {
var temp;
if ( isNumeric(obj) )
temp = findPath(data[obj], value, path + "[" + obj + "]");
else
temp = findPath(data[obj], value, path + "/" + obj);
if (temp.length) {
finalpath.push(temp);
}
}
}
return finalpath.toString();
}
var text = findPath(items, "item2");
console.log(text)