二分键 (JS) 的数据/逻辑结构

发布于 2024-12-11 12:01:27 字数 1046 浏览 1 评论 0原文

对于一个学校项目,我正在制作一个网络应用程序,它呈现一个二分键

首先,它显示可能的项目列表。用户在脑海中选择一个。然后,应用程序会向用户提出一个问题以及表示的按钮。根据对前一个问题的答复提出一个新问题。这将持续下去,直到只有一项符合用户的响应。

我的问题是如何存储数据。我最初的想法是嵌套数组。这是一个使用水果的示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯叶蝶 2024-12-18 12:01:27

我建议使用这样的表格(从技术上讲,它可以是对象数组),

                                Apple Orange Banana Pear
Is it shaped like a sphere?     +     +      -      -
Do you eat the peel/skin?       +     -      -      +
Is it yellow?                   -     -      +      -

与嵌套对象或图形相比,它更灵活,因为您可以根据选择的项目动态确定问题及其顺序。

I'd suggest using a table like this (technically, it can be an array of objects)

                                Apple Orange Banana Pear
Is it shaped like a sphere?     +     +      -      -
Do you eat the peel/skin?       +     -      -      +
Is it yellow?                   -     -      +      -

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.

离旧人 2024-12-18 12:01:27

在 JSON 中:

{
  "Question": "Is it shaped like a sphere?",
  "Yes":
    {
      "Question": "Do you eat the peel/skin?",
      "Yes": "Apple",
      "No": "Orange"
    },
  "No":
    {
      "Question": "Is it yellow?",
      "Yes": "Banana",
      "No": "Pear"
    }
}

In JSON:

{
  "Question": "Is it shaped like a sphere?",
  "Yes":
    {
      "Question": "Do you eat the peel/skin?",
      "Yes": "Apple",
      "No": "Orange"
    },
  "No":
    {
      "Question": "Is it yellow?",
      "Yes": "Banana",
      "No": "Pear"
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文