Get Union Object - Node.js Object

Node.js examples for Object:Object Operation

Description

Get Union Object

Demo Code


function getUnionObject ( /* ... */ )
{
    var res = {};
    iterateArray( arguments, function ( object )
    {//from  ww  w .  ja v  a  2 s  .  com
        copyProperties( object, res );
    });
    return res;
}


function iterateArray ( arr, callback )
{
  if ( typeof callback !== 'function' )
    throw 'Error: iterateArray function called with not a function as second argument';

  return iterate( arr.length, function ( i )
  {
    var ret = callback( arr[i], i );
    if ( ret !== undefined )
      return ret;
  });
}

function copyProperties ( sourceObject, destinationObject, propertyNames /*= getOwnProperties( sourceObject ) */)
{
  if ( propertyNames === undefined )
    propertyNames = getOwnProperties( sourceObject );
    
  iterateArray( propertyNames, function ( propertyName )
  {
    if ( sourceObject[ propertyName ] !== undefined )
      destinationObject[ propertyName ] = sourceObject[ propertyName ];
  });
}

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

Related Tutorials