1

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:

  • "train"
  • "grammar"
  • "testSeq"

and the 5 possible combinations are:

  • "train": true && "grammar": "A" && "testSeq" : 1
  • "train": null && "grammar": "A" && "testSeq" : 1
  • "train": true && "grammar": "B" && "testSeq" : 1
  • "train": null && "grammar": "B" && "testSeq" : 1
  • "train": true && "grammar": "A" && "testSeq" : 2

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.

5
  • 2
    Seems like you have a pretty good grasp of what needs to happen. What is your question? Commented May 12, 2015 at 13:30
  • Make a function with 5 empty arrays, loop trough the original array, check for the requirements, then add to the correct temporary array, Next you shuffle the 5 arrays, and at last you combine them Commented May 12, 2015 at 13:39
  • @Halcyon Basically if there is a way to extract from the array 5 chunks as defined by the rules above, then randomise each chunk separately (i.e. 5 different random sequences) and finally concatenate all the randomised chucks to rebuild an array of the same size of the original array. Commented May 12, 2015 at 13:39
  • @MaartenPeels Yes, but is there a way to avoid creating 5 different arrays and dot it on the fly? Commented May 12, 2015 at 13:41
  • @Dede uhm, i geuss it can be done, but i think it will be allot harder to make Commented May 12, 2015 at 13:42

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.