Nodejs Array Clone clone()

Here you can find the source of clone()

Method Source Code

function randomValue(min, max) {
   return Math.floor(Math.random() * (max - min + 1) + min);
};

function randomFromArray(arr) {
   return arr[Math.floor(Math.random() * arr.length)];
};

Array.prototype.clone = function() {
    var arr = this.slice(0);
    for( var i = 0; i < this.length; i++ ) {
        if( this[i].clone ) {
            //recursion
            arr[i] = this[i].clone();// ww w  . j a  va 2 s . co m
        }
    }
    return arr;
};

function shuffle(array) {
  array = array.clone();

  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

export {
   randomValue,
   randomFromArray,
  shuffle
}

Related

  1. clone()
    Array.prototype.clone = function()
        return slice.call(this, 0);
      };
    
  2. clone()
    Array.prototype.clone = function () {
      return this.slice(0);
    };
    
  3. clone()
    Array.prototype.clone = function() {
      return this.slice(0);
    };
    
  4. clone()
    Array.prototype.clone = function()
      return Array.prototype.slice.call(this, 0);
    
  5. clone()
    Array.prototype.clone = function() {
      copy = [];
      for(var i=0; i<this.length; ++i)
        copy.push(this[i]);
      return copy;
    
  6. clone()
    'use strict';
    function compose() {
        var fns = arguments;
        return function () {
            var i = fns.length-1;
            var result = fns[i--].apply(this, arguments);
            while (i >= 0) {
                result = fns[i--].call(this, result);
            return result;
        };
    Array.prototype.clone = function() {
        var result = this.slice();
        for (var i = 0; i < this.length; i++) {
            if (this[i].clone) {
                result[i] = this[i].clone();
        return result;
    var parseMoves = function (movesString) {
        var moveRegex = /([UDFBLR])(2?)('?)/g;
        var moves = [];
        var match;
        while ((match = moveRegex.exec(movesString)) !== null) {
            var face = match[1];
            var angle = (match[2]) ? Math.PI : Math.PI/2.0; 
            if (match[3]) { 
                angle = -angle;
            moves.push({ face: face, angle: angle });
        return moves;
    
  7. clone()
    Array.prototype.clone = function(){
        if ( this[0].constructor == Array ) {
            var ar, n;
            ar = new Array( this.length );
            for ( n = 0; n < ar.length; n++ ) {
                ar[n] = this[n].clone();
            return ar;
        return Array.apply( null, this );
    
  8. clone()
    Array.prototype.clone = function(){
      var ret = [];
      for (var i=0;i<this.length;i++){
        ret[i] = this[i];
      return ret;
    };
    
  9. clone()
    Array.prototype.clone = function() {
      var newArray = [];
      for (var i = 0; i < this.length; i++) {
        if (typeof(this[i]) == 'object' || typeof(this[i]) == 'function') {
          newArray[i] = this[i].clone();
        } else {
          newArray[i] = this[i];
      return newArray;
    };