【发布时间】:2020-08-27 13:43:45
【问题描述】:
我想用 javascript 和 vuejs 创建一棵树 https://fr.vuejs.org/v2/examples/tree-view.html 我很年轻,我不太了解算法。我试图做一些循环,但它不是递归的......我的想法是将属于条件部分的问题和答案放入一个对象中。
我的输出是这样的:
{
name: "Do you have a car ?",
children: [
{
name: "Yes",
children: [
{
name: "Do you have an electric car ?",
children: [
{ name: "Yes", children: [{ name: "Do you have any comments ?" }] },
{ name: "No", children: [{ name: "Do you have any comments ?" }] },
],
},
],
},
{
name: "No",
children: [
{
name: "Do you have a bicycle ?",
children: [
{ name: "Yes", children: [{ name: "Do you have any comments ?" }] },
{ name: "No", children: [{ name: "Do you have any comments ?" }] },
],
},
],
},
],
};
和我的条目:
{
id: 1,
entitled: "first question",
questions: [
{
id: 1,
entitled: "Do you have a car ?",
answers: [
{ id: 1, entitled: "Yes", conditionalSection: 2 },
{ id: 2, entitled: "No", conditionalSection: 3 },
],
},
],
},
{
id: 2,
entitled: "section yes",
questions: [
{
id: 1,
entitled: "Do you have an electric car ?",
answers: [
{ id: 1, entitled: "Yes", conditionalSection: 4 },
{ id: 2, entitled: "No", conditionalSection: 4 },
],
},
],
},
{
id: 3,
entitled: "section no",
questions: [
{
id: 1,
entitled: "Do you have a bicycle ?",
answers: [
{ id: 1, entitled: "Yes", conditionalSection: 4 },
{ id: 2, entitled: "No", conditionalSection: 4 },
],
}
],
},
{
id: 4,
entitled: "end",
questions: [
{
id: 1,
entitled: "Do you have any comments ?",
}
],
},
];
我不知道我该如何正确地写这个......
【问题讨论】:
标签: javascript algorithm vue.js