使用javascript和vuejs创建树
我想用javascript和vuejs创建一个树 我很年轻,对算法不太了解。我试着做一些循环,但它不是递归的。。。我的想法是将属于条件部分的问题和答案放入一个对象中 我的输出如下:使用javascript和vuejs创建树,javascript,algorithm,vue.js,Javascript,Algorithm,Vue.js,我想用javascript和vuejs创建一个树 我很年轻,对算法不太了解。我试着做一些循环,但它不是递归的。。。我的想法是将属于条件部分的问题和答案放入一个对象中 我的输出如下: { name: "Do you have a car ?", children: [ { name: "Yes", children: [ { name: "Do you have an el
{
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 ?",
}
],
},
];
我不知道如何正确地编写它…需要一个递归函数来将输入转换为输出之类的数据 完整的代码片段就在下面
const convert=(id)=>
条目[id]。问题。地图((问题)=>({
姓名:题为,
孩子们:!问题。答案
?{name:question.title}
:问题.答案.地图((答案)=>({
姓名:answer.com,
子项:转换(答案.条件部分-1),
})),
}))
console.log(JSON.stringify(convert(0)))
常数项=[
{
id:1,
题为“第一个问题”,
问题:[
{
id:1,
题为“你有车吗?”,
答复:[
{id:1,标题为“是”,条件节:2},
{id:2,标题为:'No',conditionalssection:3},
],
},
],
},
{
id:2,
题为“是”的章节,
问题:[
{
id:1,
题为“你有电动汽车吗?”,
答复:[
{id:1,标题为“是”,条件节:4},
{id:2,标题为:'No',conditionalssection:4},
],
},
],
},
{
id:3,
题为“第号节”,
问题:[
{
id:1,
题为“你有自行车吗?”,
答复:[
{id:1,标题为“是”,条件节:4},
{id:2,标题为:'No',conditionalssection:4},
],
},
],
},
{
id:4,
题为“结束”,
问题:[
{
id:1,
题为“你有什么意见吗?”,
},
],
},
]