Javascript Array randomize()
/**/*from w w w . j ava 2 s . co 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; }
/**/*from w w w . j av a 2s . c o m*/ * @returns {array} New array with randomized items */ Array.prototype.randomize = Array.prototype.randomize || function() { var result = []; var clone = this.slice(); while (clone.length) { var index = clone.indexOf(clone.random()); result.push(clone.splice(index, 1)[0]); } return result; }
Array.prototype.randomize = function() { if (this.length < 2) { return this; }/*from w w w .j av a 2s . c o m*/ var items = Array.from(this); var newArr = []; while (items.length > 0) { var entry = items.random(); newArr.push(entry); items.splice(items.indexOf(entry), 1); } return newArr; };
/**/*from w w w .j ava 2s .co m*/ * @returns {array} New array with randomized items * FIXME destroys this! */ Array.prototype.randomize = Array.prototype.randomize || function() { var result = []; while (this.length) { var index = this.indexOf(this.random()); result.push(this.splice(index, 1)[0]); } return result; }
/**/*from w ww. j a v a 2 s.c o m*/ * @returns {array} New array with randomized items */ Array.prototype.randomize = Array.prototype.randomize || function() { var result = []; var clone = this.slice(); while (clone.length) { var index = clone.indexOf(clone.random()); result.push(clone.splice(index, 1)[0]); } return result; };
Array.prototype.randomize = function() { if (this.length < 2) { return this; }//from w w w . ja v a2 s . c om let items = Array.from(this); let newArr = []; while (items.length > 0) { let entry = items.random(); newArr.push(entry); items.splice(items.indexOf(entry),1 ); } return newArr; };