Is it possible to shuffle a json parsed result from ajax?
$.ajax({
method: 'GET',
url: '/-pathSomething-/--ExampleOnly--',
data: { sample: sample, sample: sample, sample: sample },
success: function (result) {
var ExamDislayQNChoiceML = JSON.parse(result);
}
});
i don’t know how to do it so i don’t have much code to show..
expected output.
randomly
{ QuesNo: 3, QuesID: 3, ... }
{ QuesNo: 1, QuesID: 1, ... }
{ QuesNo: 4, QuesID: 4, ... }
{ QuesNo: 2, QuesID: 2, ... }
{ QuesNo: 5, QuesID: 5, ... }
3
Answers
this could do your request
This will be the output
use the Fisher-Yates shuffle algorithm here is a documentation link: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle and Here’s an example of how use it to shuffle the array of objects
Assuming that you wanted to shuffle array of questions objects: You can pass Math.random() to Array.sort() method. Math.random() returns float between 0 and 1. When you subtract 0.5 from result of Math.random(), the Array.sort() compare function returns random positive or negative value, so the array is shuffled: