有选择地随机化javascript对象数组

时间:2015-05-12 13:27:01

标签: javascript arrays parsing javascript-objects shuffle

我在下面发布了javascript对象数组,我需要有选择地随机选择。也就是说,我需要将数组分成块,使用shuffle函数随机化每个块,然后连接所有chuck以重建数组。有没有办法在函数中执行此操作?

块由以下字段组合定义:

  • “训练”
  • “语法”
  • “testSeq”

和5种可能的组合是:

  • “train”:true&& “语法”:“A”&& “testSeq”:1
  • “train”:null&& “语法”:“A”&& “testSeq”:1
  • “train”:true&& “语法”:“B”&& “testSeq”:1
  • “train”:null&& “语法”:“B”&& “testSeq”:1
  • “train”:true&& “语法”:“A”&& “testSeq”:2

这里是我现在拥有的数组和代码。这显然不能完成这项工作,因为它无需选择性地随机化整个数组。

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));

非常感谢。

1 个答案:

答案 0 :(得分:1)

这将创建5个不同的数组,但是可以完成这项工作,代码:

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));