1
var arr1 = [1, 2, 3, 4, 5];

How to suffle this array elements so new array contain this elements in random order without any duplicate element?

E.g: new array = [4, 1, 3, 5, 2];

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
user2672420
  • 303
  • 2
  • 4
  • 14

2 Answers2

1

As a person that used jQuery as a "golden hammer" for a long time, my advise would be to use the appropiate tool for each task. In this case, I'd use the shuffle() method in underscore.js for the task.

var new_array=_.shuffle([1, 2, 3, 4, 5, 6]);
Michał Rybak
  • 8,648
  • 3
  • 42
  • 54
ffflabs
  • 17,166
  • 5
  • 51
  • 77
1
var arr = [1,2,3,4,5,6];
arr = shuffle(arr);

function shuffle(arr) {
    for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
    return arr;
}

courtesy: http://jsfiddle.net/timur/bAynQ/

Dipu Raj
  • 1,784
  • 4
  • 29
  • 37