簡體   English   中英

如何遞歸地找到特定的對象形式樹

[英]how to find specific object form tree recursively

我想從樹下找到基於ID的單個json objec。 示例getObjeById(4)

它應該從樹下面返回obj。 需要幫助。

data={
  "mytree": {
    "id": "dectree",
    "dt": {
      "choice": {
        "id": 0,
        "title": "Which color",
        "description": "Choose color ?",
        "choice": [
          {
            "id": 1,
            "title": "Yellow",
            "description": "Yellow ? ",

            "choice": [
              {
                "id": 5,
                "title": "Dark Yellow",
                "description": "Dark Yellow ,
                "choice": [
                  {
                    "id": 6,
                    "title": "id 6 yello",
                    "description": "<span> last leaf for yello </span>"
                  }]
              },
              {
                "id": 4,
                "title": "Light Yellow",
                "description": "Light Yellow 
              }
            ]
          },
          {
            "id": 2,
            "title": "Red",
            "description": "Red ?"
          },
          {
            "id": 3,
            "title": "Green",
            "description": "Green 
          },
          {
            "id": 7,
            "title": "white",
            "description": "white color",
             "choice": [
                  {
                    "id": 8,
                    "title": "id 8 white",
                    "description": "<span> last leaf for white </span>"
                  }]
          }
        ]
      }
    }
  }
}

以下是展示遞歸搜索功能的代碼段。

請注意,此功能大約需要6毫秒來搜索該樹,大約是標准60 fps幀的三分之一。

 var data = { "mytree": { "id": "dectree", "dt": { "choice": { "id": 0, "title": "Which color", "description": "Choose color ?", "choice": [{ "id": 1, "title": "Yellow", "description": "Yellow ? ", "choice": [{ "id": 5, "title": "Dark Yellow", "description": "Dark Yellow", "choice": [{ "id": 6, "title": "id 6 yello", "description": "<span> last leaf for yello </span>" }] }, { "id": 4, "title": "Light Yellow", "description": "Light Yellow" }] }, { "id": 2, "title": "Red", "description": "Red ?" }, { "id": 3, "title": "Green", "description": "Green" }, { "id": 7, "title": "white", "description": "white color", "choice": [{ "id": 8, "title": "id 8 white", "description": "<span> last leaf for white </span>" }] }] } } } }; //Here comes the recursive function function searchTree(data, idLabel, idValue, results) { if (idLabel === void 0) { idLabel = "id"; } if (idValue === void 0) { idValue = "0"; } if (results === void 0) { results = []; } var keys = Object.keys(data); keys.forEach(function search(key) { if (typeof data[key] == "object") { results = searchTree(data[key], idLabel, idValue, results); } else { if (data[key] == idValue && key == idLabel) { results.push(data); } } }); return results; } console.log("Looking for 4:", searchTree(data, "id", "4")); console.log("Looking for 6:", searchTree(data, "id", "6")); 

編輯-平面結構

理想的結構應該看起來更像這樣:

 var data = [{ id: 1, title: "Yellow", description: "Yellow ? ", choices: [4, 5] }, { id: 2, title: "Red", description: "Red ?", choices: [] }, { id: 3, title: "Green", description: "Green", choices: [] }, { id: 4, title: "Light Yellow", description: "Light Yellow", choices: [] }, { id: 5, title: "Dark Yellow", description: "Dark Yellow", choices: [6] }, { id: 6, title: "id 6 yello", description: "<span> last leaf for yello </span>", choices: [] }, { id: 7, title: "white", description: "white color", choices: [8] }, { id: 8, title: "id 8 white", description: "<span> last leaf for white </span>", choices: [] }]; console.log("Get elements with id == 7", data.filter(function(i) { return i.id === 7 })[0]); console.log("Get elements with id == 2", data.filter(function(i) { return i.id === 1 })[0]); console.log("Get elements with id == 3 or id == 4", data.filter(function(i) { return i.id === 3 || i.id === 4 })); 

使用上述結構,使用過濾器遍歷樹變得無關緊要。 在此結構上大約需要2毫秒的計算時間,它的伸縮性應該更好。

從這里開始,我們還可以使用優化的本機功能輕松地列表進行排序或以多種方式對其進行操作。

有什么辦法可以找到immeida父表單節點? 我現在正在查看特定的示例ID:5,它可能是ID:3的一個父級的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM