Copy object by copying property by property - Node.js Object

Node.js examples for Object:Property

Description

Copy object by copying property by property

Demo Code


function copyObject( sourceObject /*= {}*/, propertyNames /*= getOwnProperties( sourceObject ) */ )
{
  if ( sourceObject === undefined )
    sourceObject = {};/*from w w  w.  jav  a2 s .  c  o m*/
  if ( propertyNames === undefined )
    propertyNames = getOwnProperties( sourceObject );

  var newObject = {};
  iterateArray( propertyNames, function ( propertyName )
  {
    newObject[ propertyName ] = sourceObject[ propertyName ];
  });
  return newObject;
}

function getOwnProperties ( object )
{
  var array = [];
  for ( var propertyName in object )
    if ( object.hasOwnProperty( propertyName ) )
      array.push( propertyName );
  return array;
}

Related Tutorials