JavaScript - 将数组拆分为具有随机数量对象的组

时间:2014-03-31 02:36:25

标签: javascript arrays split slice

我正在研究这个小应用程序,我有一个超过100个对象的数组,我想分组。棘手的部分是我希望我新创建的数组包含随机数量的对象。例如:

初始: [" a0"," a1"," a2"," a3"," a4",& #34; a5"," a6"," a7"," a8"," a9"," a10&# 34;," a11"," a12"," a13"," a14"," a15",&# 34; a16"," a17"," a18"," a19"]

改性: [" a0"," a1"," a2"],[" a3"," a4" ," a5"," a6"," a7"],[" a8"],[" a9", " A10"] ...

我想你明白了。最近几个小时我一直坚持这个,我无法弄明白。

我希望你能帮助我! 非常感谢

3 个答案:

答案 0 :(得分:5)

由于您没有为每个数组指定最小数量的数组或最小/最大元素,因此这里是一个通用函数,您可以为每个数组指定最小/最大元素。请注意,这并不严格执行min元素,实际上100%可能无法执行,具体取决于原始数组中有多少元素与每个数组元素随机分配的元素数量。例如,如果你有10个元素原始,你最小2最大4,你可能最终得到3/3/3/1,因为min将被强制执行,直到不足以强制执行。你没有真正给出任何"规则"关于那个。

minmax都是可选的,默认为1.如果仅指定了min,则max将默认为min

还要注意,因为数组是通过引用传递的,所以默认情况下这将是#34;空的"原始数组。如果要保留原始数组,请取消注释函数中的第一行(如果选择原型,则显然为n / a)。

function randChunkSplit(arr,min,max) {
  // uncomment this line if you don't want the original array to be affected
  // var arr = arr.slice();
  var arrs = [],size=1; 
  var min=min||1;
  var max=max||min||1;
  while (arr.length > 0) {
    size = Math.min(max,Math.floor((Math.random()*max)+min));
    arrs.push(arr.splice(0, size));
  }
  return arrs;
}

<强>实施例

var letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

// randChunkSplit(letters)
[["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"], ["h"], ["i"], ["j"], ["k"], ["l"], ["m"], ["n"], ["o"], ["p"], ["q"], ["r"], ["s"], ["t"], ["u"], ["v"], ["w"], ["x"], ["y"], ["z"]]

// randChunkSplit(letters,3)
[["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["p", "q", "r"], ["s", "t", "u"], ["v", "w", "x"], ["y", "z"]]

// randChunkSplit(letters,1,3)
[["a"], ["b", "c"], ["d", "e"], ["f", "g", "h"], ["i", "j", "k"], ["l"], ["m"], ["n", "o"], ["p", "q"], ["r"], ["s", "t"], ["u", "v", "w"], ["x", "y", "z"]]

// randChunkSplit(letters,2,3) 
[["a", "b", "c"], ["d", "e", "f"], ["g", "h"], ["i", "j", "k"], ["l", "m", "n"], ["o", "p", "q"], ["r", "s", "t"], ["u", "v", "w"], ["x", "y", "z"]]

// randChunkSplit(letters,2,3)
// same thing as before, but notice how this time we end up with just 1 elem left over
[["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["p", "q"], ["r", "s"], ["t", "u", "v"], ["w", "x", "y"], ["z"]]

或者,您可以根据需要将其原型化为Array,如下所示:

Array.prototype.randChunkSplit = function (min,max) {
  var arrs = [],size=1; 
  var min=min||1;
  var max=max||min||1;
  while (this.length > 0) {
    size = Math.min(max,Math.floor((Math.random()*max)+min));
    arrs.push(this.splice(0, size));
  }
  return arrs;
}

var letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

letters.randChunkSplit(1,4)

答案 1 :(得分:1)

这是您可能想要做的事情的框架:

var initial = ["a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10"];
var modifiedArrays = [];
var totalItems = 0
var arraySize = 1

while (totalItems < initial.length) {
    if ((initial.length - totalItems) <= 30)}
        arraySize = initial.length - totalItems
        totalItems += arraySize
    }
    else
    {
        arraySize = Math.floor((Math.random()*30)+1);
        totalItems += arraySize
    }

    modifiedArrays.push(initial.slice(totalItems - arraySize, arraySize + 1));
}

Diclaimer:我对JavaScript的经验很少,而且这段代码完全未经测试,所以请原谅任何错误。此外,此脚本还包含1到30个元素的块。

答案 2 :(得分:0)

我做了一些假设,即没有设定数量的数组或最小尺寸。

var original = ["a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15", "a16", "a17", "a18", "a19"];

var results = [];

var breaker = function(arr){
   if(arr.length === 0){
      return;
   }
   var pieceSize = Math.floor(Math.random()*arr.length) + 1,
       result = arr.splice(0, pieceSize);
   results.push(result);
   breaker(arr);   
}