选择性随机化对象的JavaScript数组 [英] Selectively randomise javascript array of objects

查看:119
本文介绍了选择性随机化对象的JavaScript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发布对象的下面,我需要选择随机化的JavaScript数组。也就是说,我需要在阵列划分成块,随机化与随机播放功能每个块,然后串接所有夹头重建阵列。有没有办法在一个函数来做到这一点?

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:


  • 火车

  • 语法

  • testSeq

和的5个可能的组合是:

and the 5 possible combinations are:


  • 专列:真放;&安培; 语法:A和放大器;&安培; testSeq:1

  • 专列:空&功放;&安培; 语法:A和放大器;&安培; testSeq:1

  • 专列:真放;&安培; 语法:B&放大器;&安培; testSeq:1

  • 专列:空&功放;&安培; 语法:B&放大器;&安培; testSeq:1

  • 专列:真放;&安培; 语法:A和放大器;&安培; testSeq:2

下面的阵列和code我的时刻。这显然​​是因为它与randomises没有选择性整个阵列没有做这项工作。

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

非常感谢。

推荐答案

这将创建5个不同的阵列,但会做的工作,code:

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

这篇关于选择性随机化对象的JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆