动态内容的反应状态

问题描述 投票:0回答:1

我有如下所示的数组(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",
    }
  }
]

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

javascript reactjs react-native
1个回答
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和返回每个项目的数量。现在你可以做你想做的事。

© www.soinside.com 2019 - 2024. All rights reserved.