2015-04-20 74 views
0

我有一個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" 
     } 
    } 
    ] 
} 

我想基於單個/多個密鑰(S)來搜索節點例如{ "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; 
}; 

它可以搜索基於任何一個鍵。如何修改它以使用多個鍵?我的樹的深度沒有限制。

+0

你只需要第一個對象或每個對象與這些鍵/值? – n00dl3

+0

@JuniusRendel我想要所有這樣的節點 – anu

回答

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"}] 

希望這可以幫助

+0

它適用於以上的鍵。但是,當我搜索(數據,{「id」:「6p263uh38xvnchmsrw2jn48ut」})'它不返回節點,搜索'search(data,{「id」:「2fywxwi93lg7fqggdqqpqxvpg」})'雖然返回正確。 – anu

+0

hmm。讓我檢查 – shakib

+0

第一個childResponse具有屬性名稱'id'而不是'id'。注意一個額外的'空間'。幾乎有我的頭髮拉出:) – shakib

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; 
}; 

告訴我,如果我錯了;)