Answer the question
In order to leave comments, you need to log in
How to randomly shuffle a 2D array in JS?
Hello. I am practicing using JS by doing small tests. I have an array of this type:
const questions =
[
{
question: "Какой-то вопрос",
answers:
[
{text: "Ответ", correct: true},
{text: "Ответ", correct: true},
{text: "Ответ", correct: false},
{text: "Ответ", correct: true}
]
},
{
question: "Какой-то вопрос",
answers:
[
{text: "Ответ", correct: true},
{text: "Ответ", correct: true},
{text: "Ответ", correct: false},
{text: "Ответ", correct: true}
]
},
{
question: "Какой-то вопрос",
answers:
[
{text: "Ответ", correct: true},
{text: "Ответ", correct: true},
{text: "Ответ", correct: false},
{text: "Ответ", correct: true}
]
},
]
shuffledQuestions = questions.sort(() => Math.random() - .5);
Answer the question
In order to leave comments, you need to log in
console.log(JSON.stringify(questions));
questions.forEach((e) => {
return e.answers.sort(() => Math.random() - 0.5);
});
console.log(JSON.stringify(questions));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question