使用多个键在多级json树中搜索

时间:2015-04-20 08:24:04

标签: javascript jquery json tree

我有一个带有以下结构的json树

{
    "treeId": "avhsgdkendkfhjsdoiendsj",
    "groupResponse": {
        "description": null,
        "id": "avhsgdkendkfhjsdoiendsj",
        "mAttr": null,
        "mAttrVal": null
    },
    "childResponse": [
    {
        "treeId": "6p263uh38xvnchmsrw2jn48ut",
        "childResponse": [],
        "groupResponse": {
            "description":null,
            "id ":"6p263uh38xvnchmsrw2jn48ut",
            "mAttr": "xxx",
            "mAttrVal": "222"
        }
    }, 
    {
        "treeId": "2fywxwi93lg7fqggdqqpqxvpg",
        "childResponse": [],
        "groupResponse": {
            "description ":null,
            "id": "2fywxwi93lg7fqggdqqpqxvpg",
            "mAttr": "xxx",
            "mAttrVal": "111"
        }
    }
    ]
}

我想基于单个/多个密钥搜索节点,例如{ "mAttr": "xxx", "mAttrVal","222"}

我找到了this answer

中提到的解决方案
Object.prototype.findKey = function(keyObj) {
    var p, key, val, tRet;
    for (p in keyObj) {
        if (keyObj.hasOwnProperty(p)) {
            key = p;
            val = keyObj[p];
        }
    }

    for (p in this) {
        if (p == key) {
            if (this[p] == val) {
                return this;
            }
        } else if (this[p] instanceof Object) {
            if (this.hasOwnProperty(p)) {
                tRet = this[p].findKey(keyObj);
                if (tRet) { return tRet; }
            }
        }
    }

    return false;
};

它可以根据任何单个键进行搜索。如何修改它以使用多个键?树的深度没有限制。

2 个答案:

答案 0 :(得分:1)

以下内容可能有所帮助,

var data = {
  'treeId': 'avhsgdkendkfhjsdoiendsj',
  'groupResponse': {
    'description': null,
    'id': 'avhsgdkendkfhjsdoiendsj',
    'mAttr': null,
    'mAttrVal': null
  },
  'childResponse': [
    {
      'treeId': '6p263uh38xvnchmsrw2jn48ut',
      'childResponse': [
      ],
      'groupResponse': {
        'description': null,
        'id ': '6p263uh38xvnchmsrw2jn48ut',
        'mAttr': 'xxx',
        'mAttrVal': '222'
      }
    },
    {
      'treeId': '2fywxwi93lg7fqggdqqpqxvpg',
      'childResponse': [
      ],
      'groupResponse': {
        'description ': null,
        'id': '2fywxwi93lg7fqggdqqpqxvpg',
        'mAttr': 'xxx',
        'mAttrVal': '111'
      }
    }
  ]
};

var search = function(data, qObj){
  var i, k, matched = true, set = [];
  for (k in qObj) {
    if(!qObj.hasOwnProperty(k))
      continue;
    if(!data.groupResponse.hasOwnProperty(k) || data.groupResponse[k] != qObj[k]){
      matched = false;
      break;
    }
  }
  if(matched){
    set.push(data.groupResponse);
  }
  for(i = 0; i < data.childResponse.length; i++){
    s = search(data.childResponse[i], qObj);
    Array.prototype.push.apply(set, s);
  }
  return set;
}

console.log(search(data, { "mAttr": "xxx", "mAttrVal": "222"}));

输出:

[{"description":null,"id ":"6p263uh38xvnchmsrw2jn48ut","mAttr":"xxx","mAttrVal":"222"}]

希望这会有所帮助

答案 1 :(得分:0)

我没有测试过,但我认为它应该可行

Object.prototype.findKey = function(keyObj) {
    var p,key,tRet=[],add=true;
    for (p in this) {
        if (this[p] instanceof Object) {
            if (this.hasOwnProperty(p)) {
                tRet.concat(this[p].findKey(keyObj));
            }
        }
    }
    for(key in keyObj){
        if(!this.hasOwnProperty(key)||!this[key]===keyObj[key])
            add=false;
    }
    if(add)
        tRet.push(this);
    return tRet;
};

告诉我,如果我错了;)