私はJavascriptの初心者であり、プログラミングの初心者です。私はchoice/solutionフィールドを持つ "ステップ"オブジェクトのリストを持っています。この選択/解決フィールドは、リスト内の別のオブジェクトを参照します。このフィールドに基づいてこのリストからツリー階層を作成したいと思います。ここで子供が複数の親を持つツリー構造でフラットリストを表示しますか?
は、[私は木のように再構築したい配列ファイルです:
$scope.nodes = {
"story": {
"step" : [
{
"title": "Begin",
"id": "0",
"type": "MultipleChoice",
"description": "Yo, bro it's the start of the adventure !",
"choice": [
{
"nextStepId": "1",
"#text": "You was born as a troll, no luck in life. :'("
}]
},
{
"title": "Choice",
"id": "1",
"type": "MultipleChoice",
"description": "It's time to take your life back, and choice what you should do !",
"choice": [
{
"nextStepId": "1001",
"#text": "Take an apple"
},
{
"nextStepId": "4",
"#text": "Suicide"
},
{
"nextStepId": "1001",
"#text": "You use a coin to know what to do"
}
]
},
{
"title": "Riddle",
"id": "4",
"type": "Riddle",
"description": "What is the best way to suicide ?",
"solution": {
"nextStep": "1000",
"#text": "think"
}
},
{
"title": "you are dead",
"id": "1000",
"type": "End",
"win": "true",
"description": "Nice, you are dead finally !"
},
{
"title": "you are alive",
"id": "1001",
"type": "End",
"win": "false",
"description": "Damn, you are still alive !"
}
]
}
}
、ここでは、私がこれまでにやったことです:
$scope.tree = function tree() {
var map = {}, node, roots = [];
for (var i = 0; i < nodes.length; i += 1) {
node = nodes.story.step[i];
map[node.id] = i;
if (node.id !== "0") {
switch (node.type) {
case ("MultipleChoice"):
for (var j = 0; i < node.choice.length; j += 1)
nodes[map[node.id]].choice[j].nextStepId[j].push(node);
break;
case ("Riddle"):
nodes[map[node.id]].solution.nextStep[j].push(node);
break;
case ("End"):
//TO DO
}
}
else {
roots.push(node);
}
}
}(nodes)
(/(子供という選択肢に注意してくださいソリューション)は2つ以上の親を持つことができ、その「選択」は配列または要素のいずれかになります)。
明らかに、私は何か間違っています。選択肢は「未定義」です
私を修正していただけますか?私はそれを把握することはできません。私は自分の間違いから学ぶために自分のコードを残したいと思っていますが、示唆すべきことがあれば自由に感じてください。
ありがとうございました
あなたからこれらの2以上の異なるこの質問ですか? http://stackoverflow.com/questions/37743204/constructing-a-tree-from-a-json-array-in-javascript http://stackoverflow.com/questions/37716801/constructing-a-hierarchical-tree-from -a-json-array – hatchet
はい、そうです。制約が異なる – Takichiii
同じ質問を繰り返すのをやめてください! 1つを更新しても、新しいものを繰り返し作成しないでください。あなたはちょうど数分前に同じものをもう一度尋ねてきましたが、明確化が求められた後にそれを削除しました。それはこのサイトの使い方ではありません – charlietfl