Jquery multidimensional array shuffle random

I want to minimize my code:

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

To:

 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 as follows:

 [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 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 pieces, not the values ​​in each fragment.
The result I want is as follows:

 [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 want each piece inside the array to be in the same order, but each fragment must be randomized.
Is there a way to do this using jQuery?
I also wonder how to get values ​​from a shuffled / randomized array?
At the moment, I get the following values:

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

I would suggest that I get them with something like:

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

Finally, I wonder if code minimization will work better?

+4
source share
3 answers

If you just want to start the operation on all elements of the array, you should use map or forEach . I am sure jquery provides pads for these methods in older browsers. Therefore, if we assume that you are using the original fisherYates function unchanged, we may 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); 

When accessing elements, you are almost right, but you have too many brackets:

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

I would not speculate about performance, if you are really worried, you should put together a jsperf test case.

+7
source

All you need is the jQuery.each () method, for example:

 $.each(multArr, function(i) { fisherYates(this) }); 

See console on

this working example

Fiddle Code

 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; } } $(function() { $("button").on("click", function(e) { 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]]; $.each(multArr, function(i) { fisherYates(this) }); console.log(multArr) }) }) 
+2
source

Check out my code here . Basically, they just loop around the elements of a multidimensional array and run fisherYates on them like this:

 function fisherYates(myArray) { for(var i = 0; i< myArray.length; i++) { k = myArray[i].length; while(k--){ j = Math.floor(Math.random() * (myArray.length - 1)); tempk = myArray[i][k]; tempj = myArray[i][j]; myArray[i][k] = tempj; myArray[i][j] = tempk; } } } 

Now, if you want to do this for an n-dimensional array, you will have to do it recursively, which will be fun, but I think this is more than what you requested. If not, I can update it later.

+2
source

Source: https://habr.com/ru/post/1481461/


All Articles