如何获取JavaScript对象的nodepath

时间:2014-09-05 04:08:10

标签: javascript

我有一个JavaScript对象。我怎样才能进入它?

var obj = {
  objData: {
    Name: "",
    age: "",
    Department: {
      DepartmentDetails: {
        clerk: "xyz",
        manager: "abc"
      }
    },
    WorkingDetails: [
      {
        Title: "",
        workType: "",
        Appointee: {
          BasicDetails: {
            lastName: "",
            middleName: "",
            firstName: ""
          }
        }
      }
    ]
  }
}

我尝试过以下代码。但它并没有给我正确的结果。

var x = objData.Department.DepartmentDetails.clerk;
var path = "data";
function search(path, obj, target) {
    var found = false;
    for (var k in obj) {
        if (obj.hasOwnProperty(k))
            if (obj[k] === target)
                return path + "['" + k + "']"
            else if (typeof obj[k] === "object") {
                var result = search(path + "['" + k + "']", obj[k], target);
                if (result)
                    return result;
            }
    }
    return false;
}

var path = search(path, obj, x);
console.log(path);

我期待结果为

  

Department.DepartmentDetails.clerk:xyz

对此的任何帮助都会非常有用。

2 个答案:

答案 0 :(得分:1)

这里的部分问题是有一些缺失的变量导致了逻辑问题。我对逻辑进行了一些研究,想出了一些我认为你要求的东西,只要我能正确理解这个问题。

该对象如下所示:

var obj = {
  objData: {
    Name: "",
    age: "",
    Department: {
      DepartmentDetails: {
        clerk: "xyz",
        manager: "abc"
      }
    },
    WorkingDetails: [
      {
        Title: "",
        workType: "",
        Appointee: {
          BasicDetails: {
            lastName: "",
            middleName: "",
            firstName: ""
          }
        }
      }
    ]
  }
};

新功能如下:

var path = "Department";
function search(path, obj, target) {
    var found = false;
    for (var k in obj) {
        if (obj.hasOwnProperty(k))
            if (obj[k] === target)
                return path + "." + k + ":" + target;
            else if (typeof obj[k] === "object") {
                var result = search(path + "." + k, obj[k], target);
                if (result)
                    return result;
            }
    }
    return false;
}

然后你会用这样的东西调用函数:

var path = search(path, obj.objData.Department, 'xyz');

我还创建了一个带有工作演示的JSFiddle

希望有所帮助!

答案 1 :(得分:1)

var obj = {
    objData: {
        Name: "",
        age: "",
        Department: {
            DepartmentDetails: {
                clerk: "xyz",
                manager: "abc"
            }
        },
        WorkingDetails: [{
            Title: "",
            workType: "",
            Appointee: {
                BasicDetails: {
                    lastName: "",
                    middleName: "",
                    firstName: ""
                }
            }
        }]
    }
}

    function search(obj, target) {
        var path = "";
        for (var key in obj) {
            if (obj[key] == target) {
                path += key + ":" + target;
                return path;
            } else {
                path = search(obj[key], target);
                if (path) {
                    return key+"."+path;
                }
            }
        }
    }

alert(search(obj.objData, "xyz"))

小提琴http://jsfiddle.net/vikrant47/8fk6h48c/