jeudi 20 octobre 2016

Randomly get a single item from a JSON array

How can I pull just a single item based on a data-option randomly?

For example, I have 2 questions in this array with the "value" set to 100. I want to randomly select one of these and display it using this function:

$('.unanswered').click(function(){
    var category = $(this).parent().data('category');
    var question = $(this).data('question');
    var value = map[category].questions[question].value;
    var answers = $('#answers');
    $('.modal-title').empty().text(map[category].name);
    $('#question').empty().text(map[category].questions[question].question);
    answers.empty();
    $.each(map[category].questions[question].answers, function(i, answer){
        answers.append(
            '<button class="btn btn-danger answer" ' +
                'data-category="'+category+'"' +
                'data-question="'+question+'"' +
                'data-value="'+value+'"' +
                'data-correct="'+answer.correct+'"' +
                '>'+ answer.text+'</button><br><br>'
        )
    });

Currently this displays all the questions in their relevant categories/difficulty-score level.

I have a feeling I will need to use Math.floor(Math.random * 100 to accomplish this but I am not quite sure how I would insert it?

[
    {
        "name":"category1",
        "questions":[
            {
                "value":100,
               "question":"Question 1 in category 1 for 100 points",
                "answers":[
                    {
                        "text":"A",
                        "correct":true
                    },
                    {
                        "text":"B",
                        "correct":false
                    },
                    {
                        "text":"C",
                        "correct":false
                    },
                    {
                        "text":"D",
                        "correct":false
                    }
                ]
            },
            {
                "value":100,
               "question":"Question 1 in category 1 for 100 points",
                "answers":[
                    {
                        "text":"A",
                        "correct":true
                    },
                    {
                        "text":"B",
                        "correct":false
                    },
                    {
                        "text":"C",
                        "correct":false
                    },
                    {
                        "text":"D",
                        "correct":false
                    }
                ]
            },
            {
                "value":200,
               "question":"Question 2 in category 1 for 200 points",
                "answers":[
                    {
                        "text":"A",
                        "correct":true
                    },
                    {
                        "text":"B",
                        "correct":false
                    },
                    {
                        "text":"C",
                        "correct":false
                    },
                    {
                        "text":"D",
                        "correct":false
                    }
                ]
            },
            {
                "value":300,
               "question":"Question 3 in category 1 for 300 points",
                "answers":[
                    {
                        "text":"A",
                        "correct":true
                    },
                    {
                        "text":"B",
                        "correct":false
                    },
                    {
                        "text":"C",
                        "correct":false
                    },
                    {
                        "text":"D",
                        "correct":false
                    }
                ]
            },
            {
                "value":400,
               "question":"Question 4 in category 1 for 400 points",
                "answers":[
                    {
                        "text":"A",
                        "correct":true
                    },
                    {
                        "text":"B",
                        "correct":false
                    },
                    {
                        "text":"C",
                        "correct":false
                    },
                    {
                        "text":"D",
                        "correct":false
                    }
                ]
            },
            {
                "value":500,
               "question":"Question 5 in category 1 for 500 points, daily double!",
               "double": 1,
                "answers":[
                    {
                        "text":"A",
                        "correct":true
                    },
                    {
                        "text":"B",
                        "correct":false
                    },
                    {
                        "text":"C",
                        "correct":false
                    },
                    {
                        "text":"D",
                        "correct":false
                    }
                ]
            }
        ]
    }
]




Aucun commentaire:

Enregistrer un commentaire