繁体   English   中英

选择性随机化对象的javascript数组

[英]Selectively randomise javascript array of objects

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

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

  • “培养”
  • “语法”
  • “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));

非常感谢。

这将创建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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM