Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138221

How do I randomly assign 4 ids in an array of a React Native (Javascrip) object? [duplicate]

$
0
0

This question already has an answer here:

Hi Thanks for reading.

I am struggling to show 4 answers randomly in my quiz app project now.

The data is as follows;


const questions = [
  {
    question: "What is the fifth planet from the sun?",
    answers: [
      { id: "1", text: "Mars" },
      { id: "2", text: "Jupiter", correct: true },
      { id: "3", text: "Saturn" },
      { id: "4", text: "Venus" }
    ]
  },
  {
    question: "How many planets are in the Solar System?",
    answers: [
      { id: "1", text: "6" },
      { id: "2", text: "7" },
      { id: "3", text: "8", correct: true },
      { id: "4", text: "9" }
    ]
  }
];

export default questions;

The view code is as follows;

      View
        <Text style={styles.text}>{question.question}</Text> 
        <ButtonContainer>
          {question.answers.map(answer => (
            <Button
              key={answer.id}
              text={answer.text}
              onPress={() => this.answer(answer.correct)}
            />
          ))}
        </ButtonContainer>

      View

Viewing all articles
Browse latest Browse all 138221

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>