shuffle two arrays in same order in javascript

I have two arrays var skladby=["skladba00", "skladba01", "skladba02", "skladba03"]; var zoznam=["text00", "text01", "text02", "text03"];

I would like to randomize/shuffle this two arrays in same order but items in arrays on [0] position must stay on their positions. i.e. var skladby=["skladba00", "skladba02", "skladba03", "skladba01"]; var zoznam=["text00", "text02", "text03", "text01"];

thx for your answers.

Answers


To change your arrays in place, get the shuffled order first and add the new arrangements to the end of the existing arrays.

Then splice out from index 1 to where the new arrangement begins (the original length).

The elements at index 0 are 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 */


Need Your Help

C# Server / Client Application for Universal Windows Platform (UWP) apps

c# wcf client-server win-universal-app uwp

I need to design a card game that will put on Windows Store and let people to

HTML List Box not displaying correct selected option

html mysql

I have a query that selects a list of colors from a table. The select statement is SELECT ColorDesc, ColorCode FROM Colors ORDER BY ColorCode.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.