2016-07-25 12 views
0

Ich möchte einzelne Json Objec basierend auf ID, von unten Baum zu finden. Beispiel - getObjeById(4),wie rekursiv spezifische Objektform Baum zu finden

es sollte Obj von unterhalb der Struktur zurückgeben. brauche Hilfe dabei.

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

Rekursiver Code ist sehr teuer. Kannst du deinen Baum nicht reorganisieren? –

+0

Haben Sie diese Lösung ausprobiert? - http://stackoverflow.com/questions/10679580/javascript-search-inside-a-json-object –

+0

Ich bin in Ordnung, wenn es andere Möglichkeit gibt, Objektform Baum zu finden. – user3215858

Antwort

0

Unten ist ein Ausschnitt, der eine rekursive Suchfunktion zeigt.

Wie gewarnt, benötigt diese Funktion ungefähr 6 Millisekunden, um diesen Baum zu durchsuchen, ungefähr ein Drittel eines standardmäßigen 60-fps-Rahmens.

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"));

EDIT - flache Struktur

Eine ideale Struktur richtig mehr würde wie folgt aussehen:

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

Mit einer Struktur wie oben wird das Traversieren des Baums mit filter trivial. Ca. 2 Millisekunden Rechenzeit auf dieser Struktur und es sollte viel besser skalieren.

Von hier aus könnten wir auch einfach sort unsere Liste oder manipulieren Sie es in einer Vielzahl von Möglichkeiten mit optimierten, nativen Funktionalität.

+0

gibt es noch andere Möglichkeiten, Elemente zu finden, ohne rekursiv? – user3215858

+0

Nicht ohne die 'Baum'-Struktur neu anzuordnen. Idealerweise hätten Sie eine flache Struktur mit jeder "Auswahl" auf der gleichen Ebene. –

0

Gibt es eine Möglichkeit, den Eltern-Formularknoten zu finden? Ich bin jetzt speziell Beispiel ID: 5 und es kann Teil eines Elternteils sein, die ID ist: 3.