New array with randomized items - Node.js Array

Node.js examples for Array:Random Array

Description

New array with randomized items

Demo Code


/**//from   w  ww.j a  v  a2 s.c o m
 * @returns {array} New array with randomized items
 * FIXME destroys this!
 */
Array.prototype.randomize = function() {
  var result = [];
  while (this.length) {
    var index = this.indexOf(this.random());
    result.push(this.splice(index, 1)[0]);
  }
  return result;
}

Related Tutorials