I need to find a way to reformat and/or make a function that will take an object and select a random key:value from it, ensuring that the information I need from it is still accessible. Currently (to make sure everything else I had was working), I am just going through all of the object.
Right now I have this object (shortened for simplicity):
const questions = {
1:{
"What is the capital city of The United States of America?":{
"London":false,
"Richmond":false,
"Washington DC":true,
"Paris":false
},
"In what continent is Bangladesh located?":{
"North America":false,
"Africa":false,
"Europe":false,
"Asia":true
}
},
First key is what I've been using to organize each question by point value, then each question has each multiple choice answer and whether it is correct or incorrect. Any changes to the object's formatting are welcome, and informative responses would be appreciated since I am relatively knew to JS. link to my repl
The best you can do is to create an array and put all your objects inside it. And while fetching, you can use random function to select any random object from the array.
questions : [ {}, {}, {} ]
const randomQuestion = Math.floor(Math.random() * questions.length);