Reputation: 127
I have the javascript array of objects posted below which I need to randomise selectively. That is, I need to divide the array in chunks, randomise each chunk with the shuffle function and then concatenate all the chucks to rebuild the array. is there any way to do this in a function?
Chunks are defined by a combination of the following fields:
and the 5 possible combinations are:
Here the array and the code I have at the moment. This obviously doesn't do the job because it randomises the whole array with no selectivity.
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var myArray = [
{
"trial" : {
"index": 0,
"word": "WORD 1",
"keyboard": true,
"train": true,
"test": null,
"grammatical": true,
"grammar" : "A",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 2",
"keyboard": true,
"train": true,
"test": false,
"grammatical": true,
"grammar" : "A",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 3",
"keyboard": true,
"train": false,
"test": true,
"grammatical": true,
"grammar" : "A",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
},
"trial" : {
"index": 0,
"word": "WORD 1",
"keyboard": true,
"train": true,
"test": null,
"grammatical": true,
"grammar" : "B",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 2",
"keyboard": true,
"train": true,
"test": false,
"grammatical": true,
"grammar" : "B",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 3",
"keyboard": true,
"train": false,
"test": true,
"grammatical": true,
"grammar" : "B",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 3",
"keyboard": true,
"train": false,
"test": true,
"grammatical": true,
"grammar" : "B",
"testSeq" : 2
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
}
];
console.log(shuffle(myArray));
Thanks a lot.
Upvotes: 1
Views: 69
Reputation: 1022
This will create 5 different arrays, but will do the job, code:
function shuffle(array) {
var temp = {
arr1: [],
arr2: [],
arr3: [],
arr4: [],
arr5: []
};
for (var i=0; i<array.length; i++) {
var cur = array[i].trial;
if(cur.train && cur.grammar == "A" && cur.testSeq == 1) temp.arr1.push(array[i]);
else if(!cur.train && cur.grammar == "A" && cur.testSeq == 1) temp.arr2.push(array[i]);
else if(cur.train && cur.grammar == "B" && cur.testSeq == 1) temp.arr3.push(array[i]);
else if(!cur.train && cur.grammar == "B" && cur.testSeq == 1) temp.arr4.push(array[i]);
else if(cur.train && cur.grammar == "A" && cur.testSeq == 2) temp.arr5.push(array[i]);
}
for(var j=0; j<temp.length; j++)
{
var curArr = temp[i];
var currentIndex = curArr.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = curArr[currentIndex];
curArr[currentIndex] = curArr[randomIndex];
curArr[randomIndex] = temporaryValue;
}
}
return temp.arr1.concat(temp.arr2, temp.arr3, temp.arr4, temp.arr5);
}
var myArray = [
{
"trial" : {
"index": 0,
"word": "WORD 1",
"keyboard": true,
"train": true,
"test": null,
"grammatical": true,
"grammar" : "A",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 2",
"keyboard": true,
"train": true,
"test": false,
"grammatical": true,
"grammar" : "A",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 3",
"keyboard": true,
"train": false,
"test": true,
"grammatical": true,
"grammar" : "A",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
},
"trial" : {
"index": 0,
"word": "WORD 1",
"keyboard": true,
"train": true,
"test": null,
"grammatical": true,
"grammar" : "B",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 2",
"keyboard": true,
"train": true,
"test": false,
"grammatical": true,
"grammar" : "B",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 3",
"keyboard": true,
"train": false,
"test": true,
"grammatical": true,
"grammar" : "B",
"testSeq" : 1
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
},
{
"trial" : {
"index": 1,
"word": "WORD 3",
"keyboard": true,
"train": false,
"test": true,
"grammatical": true,
"grammar" : "B",
"testSeq" : 2
},
"metadata" : {
"instructions" : "Type in the word shown as quickly as possible",
"submitUrl" : "/server/trialSubmit"
}
}
];
console.debug(shuffle(myArray));
Upvotes: 1