jquery多维数组shuffle随机 [英] jquery multidimensional array shuffle random

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

问题描述

我想最小化我的代码:

myArrayA = [1, 2, 3, 4, 5];
fisherYates(myArrayA);
myArrayB = [6, 7, 8, 9, 10];
fisherYates(myArrayB);
myArrayC = [11, 12, 13, 14, 15];
fisherYates(myArrayC);
myArrayD = [16, 17, 18, 19, 20];
fisherYates(myArrayD);
myArrayE = [21, 22, 23, 24, 25];
fisherYates(myArrayE);

收件人:

var multArr = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]];
fisherYates(multArr);

我想要的输出是这样的:

The output I want is like this:

[4,2,3,5,1],[7,10,6,9,8],[11,15,12,14,13],[18,17,16,20,19],[22,21,25,23,24]

I试过这段代码:

http://jsfiddle.net/arrow/yFn8U/

I tried this code:
http://jsfiddle.net/arrow/yFn8U/

function fisherYates(myArray) {
var i = myArray.length, j, tempi, tempj;
if (i === 0) return false;
while (--i) {
    j = Math.floor(Math.random() * (i + 1));
    tempi = myArray[i];
    tempj = myArray[j];
    myArray[i] = tempj;
    myArray[j] = tempi;
}
}
var multArr = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]];
fisherYates(multArr);

但我的代码只是随机化了块的顺序而不是每个块中的值。

我想要的输出是这样的:

But my code only randomizes the order of the chunks not the values in each chunk.
The output I want is like this:

[4,2,3,5,1],[7,10,6,9,8],[11,15,12,14,13],[18,17,16,20,19],[22,21,25,23,24]

我希望数组中的每个块都处于相同的顺序,但每个块必须随机化。

有没有办法用jQuery做这个?

我也想知道如何从洗牌/随机数组中获取值?

目前我得到的值就像这个:

I want each chunk inside the array to be in the same order but each chunk must be randomized.
Is there a way to do this with jQuery?
I also wonder how to get values from the shuffled/randomized array?
At the moment I get the values like this:

myArrayA[i]
myArrayB[i]
myArrayC[i]
myArrayD[i]
myArrayE[i]

我猜我会用东西给他们喜欢:

I would guess I will get them with something like:

multArr [[0][i]];
multArr [[1][i]];
multArr [[2][i]];
multArr [[3][i]];
multArr [[4][i]];

最后我想知道最小化代码会带来更好的性能吗?

Finally I wonder if minimizing the code will give better performance?

推荐答案

如果你只是想对一个数组中的所有元素运行一个操作,那么你应该使用 map 的forEach 。我确定jquery在旧浏览器中为这些方法提供了填充程序。因此,如果我们假设您使用原始的 fisherYates 函数,我们可能会有这样的结果:

If you simply want to run an operation over all the elements in an array, then you should use map or forEach. I'm sure jquery provides shims for these methods in older browsers. So if we assume you're using your original fisherYates function unaltered, we might have something like this:

var multArr = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]];
multArr.forEach(fisherYates);

在访问元素时,你几乎是正确的,但你有一组括号太多:

On accessing the elements, you're almost right, but you have one set too many of brackets :

multArr[1]; // == [6, 7, 8, 9, 10]
multArr[1][3]; // == 9

如果你真的担心你,我不会猜测性能应该把一个jsperf测试用例放在一起。

I wouldn't speculate about the performance, if you're really worried you should put together a jsperf test case.

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

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