Check if a property exists in an object - Node.js Object

Node.js examples for Object:Property

Description

Check if a property exists in an object

Demo Code


function canGetProperty ( /* object, propertyName1, ... */ )
{
  return tryGetProperty.apply( this, arguments ) !== undefined;
}

function tryGetProperty ( object /*, propertyName1, ... */ )
{
  for ( var i = 1; i < arguments.length; i += 1 )
  {/*from www.jav  a2s.  c o  m*/
    if ( object === undefined )
      return;
    var propertyName = arguments[ i ];
    object = object[ propertyName ];
  }
  return object;
}

Related Tutorials