shuffle two arrays in same order in javascript -
i have 2 arrays
var skladby=["skladba00", "skladba01", "skladba02", "skladba03"];
var zoznam=["text00", "text01", "text02", "text03"];
i randomize/shuffle 2 arrays in same order items in arrays on [0] position must stay on positions.
i.e.
var skladby=["skladba00", "skladba02", "skladba03", "skladba01"];
var zoznam=["text00", "text02", "text03", "text01"];
thx answers.
to change arrays in place, shuffled order first , add new arrangements end of existing arrays.
then splice out index 1 new arrangement begins (the original length).
the elements @ index 0 not affected.
var skladby= ["skladba00", "skladba01", "skladba02", "skladba03"]; var zoznam= ["text00", "text01", "text02", "text03"]; var i=0, len= skladby.length, next, order=[]; while(i<len)order[i]= ++i; //[1,2,3...] order.sort(function(){return math.random()-.5}); for(i= 0; i<len; i++){ next= order[i]; skladby.push(skladby[next]); zoznam.push(zoznam[next]); } skladby.splice(1, len); zoznam.splice(1, len);
/* returned values
skladba00,skladba03,skladba01,skladba02
text00,text03,text01,text02 */
Comments
Post a Comment