JavaScript获取问卷所有答题路径及现有代码问题排查
问卷有效答题路径遍历问题修复
问题根源
原有代码存在以下核心错误导致路径重复、分支未拆分:
- 错误的递归截断条件:
if (questionNum > 5) { return false; }直接截断了题号大于5的所有路径,根本无法到达最终题12 - 路径对象共享复用:多个分支修改同一个路径数组,没有为每个独立分支创建路径副本,导致节点堆叠重复
- 结果收集逻辑错误:没有在到达最终题12时将完整路径存入结果集,嵌套数组结构的设计也不符合路径收集的需求
- 分支拆分逻辑错误:所有分支共用同一个基础路径索引,没有为每个选项分支生成独立的路径条目
修复后可直接运行的代码
const originalQuestions = { 1: { title: "Title", firstQuestion: true, options: [{ tooltip: "", nextQuestion: 2 }, { tooltip: "", nextQuestion: 2 }, { tooltip: "", nextQuestion: 10000 }] }, 2: { title: "Title", options: [{ tooltip: "", nextQuestion: 3 }, { tooltip: "", nextQuestion: 3 }, { tooltip: "", nextQuestion: 3 }, { tooltip: "", nextQuestion: 3 }] }, 3: { title: "Title", options: [{ tooltip: "", nextQuestion: 4 }, { tooltip: "", nextQuestion: 4 }, { tooltip: "", nextQuestion: 4 }] }, 4: { title: "Title", options: [{ tooltip: "", nextQuestion: 13 }, { tooltip: "", nextQuestion: 5 }] }, 5: { title: "Title", options: [{ tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 10000 }] }, 6: { title: "Title", options: [{ tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 14 }] }, 7: { title: "Title", options: [{ tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }] }, 8: { title: "Title", options: [{ tooltip: "", nextQuestion: 9 }, { tooltip: "", nextQuestion: 9 }, { tooltip: "", nextQuestion: 9 }] }, 9: { title: "Title", options: [{ tooltip: "", nextQuestion: 10 }, { tooltip: "", nextQuestion: 10 }, { tooltip: "", nextQuestion: 10 }] }, 10: { title: "Title", options: [{ tooltip: "", nextQuestion: 11 }, { value: "Roof", attribute: "Flue Exit", tooltip: "", nextQuestion: 15 }] }, 11: { title: "Title", options: [{ tooltip: "", nextQuestion: 12 }, { tooltip: "", nextQuestion: 12 }] }, 12: { finalQuestion: true, input: true, placeHolder: 'e.g SWS' }, 13: { title: "Title", options: [{ tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 6 }] }, 14: { title: "Title", options: [{ tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 10000 }] }, 15: { title: "Title", options: [{ tooltip: "", nextQuestion: 12 }, { tooltip: "", nextQuestion: 12 }] }, 17: { title: "Title", options: [{ tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }] }, 10000: { isError: true, title: "Finally, what is the first part of your postcode?", error: "Postcode" } }; // 存储所有有效路径的结果数组 const validPaths = []; function traverse(currentQuesNum, currentPath) { const currentQues = originalQuestions[currentQuesNum]; // 到达最终题,将当前路径存入结果集 if (currentQues.finalQuestion) { validPaths.push([...currentPath]); return; } // 过滤掉跳转错误页的选项 const validOptions = currentQues.options.filter(opt => opt.nextQuestion !== 10000); // 遍历每个有效选项,生成独立分支路径 validOptions.forEach(opt => { const nextQuesNum = opt.nextQuestion; const newPath = [...currentPath, nextQuesNum]; traverse(nextQuesNum, newPath); }) } // 从首题1开始遍历,初始路径为[1] traverse(1, [1]); // 输出所有有效路径 console.log(validPaths); // 可选去重操作:const uniquePaths = [...new Set(validPaths.map(p => p.join(',')))].map(p => p.split(',').map(Number));
逻辑说明
- 每个选项分支生成独立的路径副本,分支之间的修改完全隔离,不会出现节点堆叠
- 移除了错误的题号截断逻辑,所有合法跳转都会正常遍历
- 到达标记为
finalQuestion的12题时自动将完整路径存入结果集 - 自动过滤所有跳转到10000错误页的选项,不会纳入有效路径统计
内容的提问来源于stack exchange,提问作者YaBCK
相关产品推荐
相关产品推荐

