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

有选择地随机化javascript对象数组,javascript,arrays,parsing,javascript-objects,shuffle,Javascript,Arrays,Parsing,Javascript Objects,Shuffle,我在下面发布了javascript对象数组,我需要有选择地随机选择。也就是说,我需要将数组划分为块,使用shuffle函数随机分配每个块,然后连接所有的卡盘以重建数组。在函数中有什么方法可以做到这一点吗 块由以下字段的组合定义: “火车” “语法” “testSeq” 5种可能的组合是: “train”:true&&“grammar”:“A”&&&“testSeq”:1 “train”:null&&“grammar”:“A”&&&“testSeq”:1 “train”:true和“gram

我在下面发布了javascript对象数组,我需要有选择地随机选择。也就是说,我需要将数组划分为块,使用shuffle函数随机分配每个块,然后连接所有的卡盘以重建数组。在函数中有什么方法可以做到这一点吗

块由以下字段的组合定义:

  • “火车”
  • “语法”
  • “testSeq”
5种可能的组合是:

  • “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
这里是我目前拥有的数组和代码。这显然不起作用,因为它没有选择性地随机化整个数组

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

非常感谢。

这将创建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));
函数洗牌(数组){
变量温度={
arr1:[],
arr2:[],
arr3:[],
arr4:[],
arr5:[]
};

对于(var i=0;我想你已经很好地掌握了需要发生什么。你的问题是什么?用5个空数组创建一个函数,在原始数组中循环,检查要求,然后添加到正确的临时数组中,然后洗牌5个数组,最后合并them@Halcyon基本上,如果有一种方法可以从t他按照上述规则排列5个区块,然后分别随机化每个区块(即5个不同的随机序列)最后将所有随机的卡盘连接起来,以重建一个与原始阵列大小相同的阵列。@MaartenPeels是的,但有没有办法避免创建5个不同的阵列并在运行中对其进行点阵?@Dede uhm,我认为这是可以做到的,但我认为分配会更难