jquery - Build dynamic multi-dimensional array -
i'm trying build multi-dimensional array dynamically.
the reason want build dynamically if array grows 1 - 1000 in 5-number chunks.
time consuming write this:
[1, 2, 3, 4, 5],,,,,[996, 997, 998, 999, 1000]
i've been struggling whole day today, decided post question because i'm totally stuck right now.
this array want build dynamically (my earlier post solved):
multi-dimensional array shuffle random
once dynamic array built properly, want call fisheryates() function 'outerarr.foreach(fisheryates);' result 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]
array used fadeout/fadein pictures.
1. fadein first set of 5 random pictures 1-5
2. fadeout first set
3. fadein second set of 5 random pictures 6-10
4. fadeout second set
5. fadein third set of 5 random pictures 11-15
6. , on....
i use array values this:
$currimg = $('.rotator-image:visible', $currli); $next = $('.img' + outerarr[a][b], $currli); $currimg.fadeout(1000); $next.fadein(1000);
i've tried solved of these links:
jquery.map
create multidimentional array dynamic form
pointy's code in this post.
some notes: prefer not use "var outerarr = new array();"
. read somwhere should avoided(?). accomplish jquery way, .push , $.makearray (and $.map) if that's possible. however, approach appreciated.
here code have (increase javascript window in jsfiddle see comments): (and here)
function fisheryates(myarray) { var = 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 outerarr = []; var innerarr = []; var fakearr = 0; var zz = 0; (var z = 1; z < 26; ++z) { ++zz; if (zz != 5) { fakearr = fakearr + z + ","; } else { fakearr = fakearr + z; var realarr = $.makearray(fakearr); innerarr.push(realarr); outerarr.push(innerarr); innerarr = []; innerarr.length = 0; fakearr = ""; fakearr.length = 0; zz = 0; } } // shuffle/randomize numbers in each chunk not order of 5 chunks outerarr.foreach(fisheryates); alert(outerarr);
the problem when want values array. don't single values (like outerarr[1][3] should show 9). each full chunk (like 6,7,8,9,10). belive have use $.map don't know how use $.map example. shuffle/random function (i.e. outerarr.foreach(fisheryates);
) doesn't work either code right now.
the array should randomized (as explained in first link @ top) should able shuffle/random working once dynamic part working.
you on wrong track idea should jquery. kind of code nothing jquery at. jquery dom manipulation library, not general-purpose javascript library kind of coding.
if want library useful these kinds of array manipulations, place start underscore.js or similar newer lo-dash. these libraries have kinds of array , object manipulation functions. review documentation both these libraries , see if helpful. (after reading docs, if you're not sure pick, wouldn't go wrong either one, suggestion lo-dash.)
otherwise, should write bare javascript code, old fashioned loops , such things.
some notes: prefer not use
var outerarr = new array();
. read somwhere should avoided(?).
what you're using instead var outerarr = [];
. that's fine, , indeed recommended practice these days. cosmetic difference. new array()
, []
mean same thing , work identically. changing 1 other wouldn't cause problems or fix problems.
also, indentation in code mess. impossible tell statements nested inside what. took liberty of cleaning indentation. please check , make sure got right.
i see there fisheryates()
function in code, function never called. not sure about, perhaps can clarify.
what fake array for? why need fake array , real array?
this code has 2 statements nothing:
innerarr = []; innerarr.length = 0; // nothing fakearr = ""; fakearr.length = 0; // nothing
both innerarr
, fakearr
have length of 0 because values []
, ""
respectively, there's no need set length
0 again.
and 1 final suggestion: perhaps instead of trying write out javascript code immediately, ought write out in english - in painstaking detail - input , output , steps think there. you're partway there description posted, there lot of details missing. if can write out of details, either in english or in pseudocode, may make easier write actual code.
i know doesn't answer question, should give food thought! :-)
Comments
Post a Comment