-1

I'm trying to display one random propertie from an object instead all the properties it has.

I have that object:

let questions = [{
        letter: "a",
        answer: {
            a1: "abducir",
            a2: "abadía",
            a3: "abandonar",
        },
        status: 0,
        question: {
            q1: "CON LA A. Dicho de una supuesta criatura extraterrestre: Apoderarse de alguien",
            q2: "CON LA A. Templo o monasterio que gobierna un abad o una abadesa.",
            q3: "CON LA A. Dejar solo, sin atención, sin cuidados a una persona, un animal o una cosa."
        }
    },
    {
        letter: "b",
        answer: {
            a1: "bingo",
            a2: "babor",
            a3: "bacteria",
        },
        status: 0,
        question: {
            q1: "CON LA B. Juego que ha sacado de quicio a todos los 'Skylabers' en las sesiones de precurso",
            q2: "CON LA B. Parte izquierda de un barco.",
            q3: "CON LA B. Organismo muy pequeño que a veces produce enfermedades."
        }
    },
    {
        letter: "c",
        answer: {
            a1: "churumbel",
            a2: "cañón",
            a3: "caballo",

        },
        status: 0,
        question: {
            q1: "CON LA C. Niño, crío, bebé",
            q2: "CON LA C. Arma de gran tamaño con forma de tubo. Dispara proyectiles de hierro con forma de bola.",
            q3: "CON LA C. Pieza del juego del ajedrez."
        }
    },
]

And I have that function that returns the properties inside question:

function showQuestions() {

    for (let i = 0; i < questions.length; i++) {

        let questionsObject = questions[i].question;

        for (var quest in questionsObject) {
            console.log(questionsObject[quest])
        }  
    }

}
showQuestions();

I am obtaining that data:

enter image description here

How instead of getting all A, B and C, can I get one A, one B and one C, randomly? Like this:

CON LA A. Templo o monasterio que gobierna un abad o una abadesa.

CON LA B. Organismo muy pequeño que a veces produce enfermedades.

CON LA C. Niño, crío, bebé"

Thank you!

S.Marx
  • 977
  • 10
  • 14

1 Answers1

2

Already answered here.

const keys = Object.keys(questionsObject);
const randomQuestion = questionsObject[keys[keys.length * Math.random() << 0]];
Reqven
  • 1,688
  • 1
  • 8
  • 13