Nodejs Array Clone clone(deep)

Here you can find the source of clone(deep)

Method Source Code

'use strict';/*from   ww w  . j a v a2s .com*/

// Clone kit

function clone_array(src, deep) {
   var result = [];
   var i, l = src.length;

   for (i = 0; i < l; i++) {
      if (deep)
         result.push(clone(src[i], deep));
      else
         result.push(src[i]);
   }

   return(result);
}

function clone_object(src, deep) {
   var result = {};

   Object.forEach(src, function(key, val) {
      if(deep)
         result[key] = clone(val, deep);
      else
         result[key] = val;
   });
   
   return(result);
}

function clone(src, deep) {
   return(Array.isArray(src) ? clone_array(src, deep) : (Object.isObject(src) ? clone_object(src, deep) : src));
}

// Globals
Array.prototype.clone = function(deep) {
   return(clone_array(this, deep));
}

Object.clone = clone_object;

module.exports = clone;
module.exports.object = clone_object;
module.exports.array = clone_array;

Related

  1. clone()
    Array.prototype.clone = function () {
        return this.slice(0);
    };
    
  2. clone()
    Array.prototype.clone = function () {
      var a = new Array(); 
      for (var property in this) {
        a[property] = typeof (this[property]) == 'object' ? this[property].clone() : this[property]
      return a
    
  3. clone()
    Array.prototype.clone = function() {
      return this.concat();
    
  4. clone()
    Array.prototype.clone = function() {
      var clone = [];
      for (var i = 0; i < this.length; i++) {
        clone.push(this[i]);
      return clone;
    };
    
  5. clone()
    Array.prototype.clone = function(){
      var ret = new Array(this.length);
      for(var i=0;i<this.length;i++){
        if(this[i].constructor==Array){
          ret[i]=this[i].clone();
        }else{
          ret[i]=this[i];
      return ret;
    
  6. cloneReserva()
    Array.prototype.cloneReserva = function() {
       var arr = [];
       for(var i = 0; i < this.length ; i++){
           obj = {
             reservaId   : this[i].reservaId,      
              color       : this[i].color, 
              timeStart   : new Date(this[i].timeStart),  
              timeEnd     : new Date(this[i].timeEnd),  
              reservedBy  : this[i].reservedBy, 
    ...
    
  7. Clone()
    Array.prototype.Clone = function()
        var n = [];
        var s = this.constructor;
        for ( var i in this )
            if ( !s[ i ] )
                if ( this[ i ] != null && typeof this[ i ] == typeof [] )
                    n[ i ] = this[ i ].Clone();
    ...
    
  8. _clone()
    Array.prototype._clone = function() {
        var ar = [];
        for(var i = 0; i < this.length; i++) {
            ar.push(this[i]);
        return ar;
    
  9. deepClone()
    Array.prototype.deepClone = function () {
      return JSON.parse(JSON.stringify(this));
    };