Example:
var someObject = {
\'part1\' : {
\'name\': \'Part 1\',
\'txt\': \'example\',
},
\'part2\' : {
\'name\': \'Part 2\',
\'size\': \'15\',
Try this:
http://jsfiddle.net/n8d2s/1/
The method returns an object, that contains the parts.
function getPath(obj, value, path){
path = path || [];
if(obj instanceof Array || obj instanceof Object){
//Search within children
for(var i in obj){
path.push(i); //push path, will be pop() if no found
var subPath = getPath(obj[i], value, path);
//We found nothing in children
if(subPath instanceof Array){
if(subPath.length == 1 && subPath[0]==i){
path.pop();
}
//We found something in children
}else if(subPath instanceof Object){
path = subPath;
break;
}
}
}else{
//Match ?
if(obj == value){
return {finalPath:path};
}else{
//not ->backtrack
path.pop();
return false;
}
}
return path;
}
Maybe its not the best code, took me 10min there but it should work.
To see any result, check your dev console F12.