Example:
var someObject = {
\'part1\' : {
\'name\': \'Part 1\',
\'txt\': \'example\',
},
\'part2\' : {
\'name\': \'Part 2\',
\'size\': \'15\',
If you try this on the window
object you'll end up in trouble as it is self referential. Quick and easy way to get around this is to a depth check:
function getPath(obj, val, path, depth) {
if (!depth) depth = 0;
if (depth > 50) return '';
path = path || "";
var fullpath = "";
for (var b in obj) {
if (obj[b] === val) {
return (path + "/" + b);
}
else if (typeof obj[b] === "object") {
fullpath = getPath(obj[b], val, path + "/" + b, depth++) || fullpath;
}
}
return fullpath;
}
This will return the same kind of path as above:
/part3/2/qty
To make that into an actual reference to variable simply regex:
'window' + '/part3/2/qty'.replace(/\/([0-9]+)\//g, '[$1].').replace(/\//g, '.')