I've got an array that I want to re order based on the index of an item in the array
I've tried two different methods so far but it seems that the array comes out just as when it was sent in.
function orderPlayers(players, fpIndex){
for(var i = 0; i < players.length; i ++ ){
players.move(i, fpIndex);
fpIndex = fpIndex + 1;
if(fpIndex > players.length - 1){fpIndex = 0;}
}
return players;
}
Array.prototype.move = function (from, to) {
this.splice(to, 0, this.splice(from, 1)[0]);
};
Example of data:
fpIndex: 1 //The player that is first is at array index 1
Players: {
player: {
age: 19,
name: Bob,
piece: "red",
isFirst: false
}
player: {
age: 21,
name: Steve,
piece: "blue",
isFirst: true
}
}
Should come out as:
Players: {
player: {
age: 21,
name: Steve,
piece: "blue",
isFirst: true
}
player: {
age: 19,
name: Bob,
piece: "red",
isFirst: false
}
}