二分键 (JS) 的数据/逻辑结构
对于一个学校项目,我正在制作一个网络应用程序,它呈现一个二分键。
首先,它显示可能的项目列表。用户在脑海中选择一个。然后,应用程序会向用户提出一个问题以及表示是
或否
的按钮。根据对前一个问题的答复提出一个新问题。这将持续下去,直到只有一项符合用户的响应。
我的问题是如何存储数据。我最初的想法是嵌套数组。这是一个使用水果的示例:
Options: Apple, Orange, Banana, Pear
0 - "Is it shaped like a sphere?" Initial question
1 - Yes response
0 - "Do you eat the peel/skin?" Subsequent question
1 - "Apple" Answer based on Yes response
2 - "Orange" Answer based on No response
2 - No response to initial
0 - "Is it yellow Subsequent question
1 - "Banana" Answer based on Yes response
2 - "Pear" Answer based on No response
但是,这种方式对于大量数据来说似乎会变得相当笨重。有没有更好的方法来构建这个?
这必须完全是 JS/HTML;我不想使用数据库或类似的东西。
For a school project, I'm making a webapp that presents a dichotomous key.
First, it shows a list of possible items. The user picks one in their head. The app then presents the user with a question and buttons for Yes
or No
. A new question is asked based on the response to the previous one. This continues until only one item fits the user's responses.
My question is how to store the data. My initial idea was a nested array. Here's an example using fruits:
Options: Apple, Orange, Banana, Pear
0 - "Is it shaped like a sphere?" Initial question
1 - Yes response
0 - "Do you eat the peel/skin?" Subsequent question
1 - "Apple" Answer based on Yes response
2 - "Orange" Answer based on No response
2 - No response to initial
0 - "Is it yellow Subsequent question
1 - "Banana" Answer based on Yes response
2 - "Pear" Answer based on No response
But, this way seems like it could get pretty unwieldy with a lot of data. Is there a better way to structure this?
This has to be completely JS/HTML; I don't want to use a database or anything like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用这样的表格(从技术上讲,它可以是对象数组),
与嵌套对象或图形相比,它更灵活,因为您可以根据选择的项目动态确定问题及其顺序。
I'd suggest using a table like this (technically, it can be an array of objects)
It is more flexible compared to nested objects or graphs because you can determine questions and their order dynamically, depending on what items are selected.
在 JSON 中:
In JSON: