反应状态以获取动态内容

时间:2018-09-02 05:11:10

标签: javascript reactjs react-native

我有如下所示的数组(5-8),如何使用react seState选择要为每个单独的数组返回的问题(对象)的数量。

const topic = [{
    questions: "The one question is here",
    answer: "A",
    answers: {
      a: "this is option A",
      b: "this is option B",
      c: "this is option C",
      d: "this is option D",
    }
  },
  {
    questions: "The two question is here",
    answer: "A",
    answers: {
      a: "this is option A",
      b: "this is option B",
      c: "this is option C",
      d: "this is option D",
    }
  }
]

)实际选择每个数组要返回的对象数

1 个答案:

答案 0 :(得分:0)

我真的不知道您想要什么,但是根据我的理解,您可以为每个可以使用Id的问题分配一个uuid,例如:

const topic = [{
    id:1,
    questions: "The one question is here",
    answer: "A",
    answers: {
      a: "this is option A",
      b: "this is option B",
      c: "this is option C",
      d: "this is option D",
    }
  },
  {
    id:2,
    questions: "The two question is here",
    answer: "A",
    answers: {
      a: "this is option A",
      b: "this is option B",
      c: "this is option C",
      d: "this is option D",
    }
  }
]

现在每个问题的ID都可以设置,例如

state={
  {id:1,count:2},
  {id:2,count:3}
}

现在您有了每个问题的ID和返回的每个项目的数量。现在你可以做你想做的。