2010-02-12

Compare Two Objects for Matching Properties


hasMatchingProperty

I've been way too busy with a large project recently to post anything here, but I created this nice function to compare properties of two objects. It would be great as an extension to the Object class if you felt so inclined.

Note, with the way the properties are compared using three equal signs === there is no need to do a separate verification that the property of each object is of the same variable type. The === is only true if the variable type is the same without any coercion.

If you wanted to check every property of the two objects then you would need a little more code to iterate through the properties of one object with the properties of another object. I set that up, but alas didn't write it as a generic function to share.

//
function hasMatchingProperty ( o1 , o2 , pn ) {
//-------------------------------------------------------------------------
//-- H A S M A T C H I N G P R O P E R T Y
//-------------------------------------------------------------------------
//-- Generic: Yes! should work for any ECMA 2.62 based language including
//-- JavaScript and ExtendScript
//-------------------------------------------------------------------------
//-- Purpose: To determing if two objects possess the same property and the
//-- value of that property is an exact match with now cooresion. If
//-- neither has that property then the result is also true -- the value
//-- matches for each object
//-------------------------------------------------------------------------
//-- Arguments:
//-- o1: The first object
//-- o2: The second object
//-- pn: The string name of the property
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: a Boolean true if the objects have matching properties, else
//-- false
//-------------------------------------------------------------------------
//-- Sample Use:
//~ var a = hasMatchingProperty ( {A:"z", C:0, E:false} , {B:"Y", C:0, E:0} , 'A' ) ;//=false
//~ var b = hasMatchingProperty ( {A:"z", C:0, E:false} , {B:"Y", C:0, E:0} , 'B' ) ;//=false
//~ var c = hasMatchingProperty ( {A:"z", C:0, E:false} , {B:"Y", C:0, E:0} , 'C' ) ;//=true
//~ var d = hasMatchingProperty ( {A:"z", C:0, E:false} , {B:"Y", C:0, E:0} , 'D' ) ;//=true
//~ var e = hasMatchingProperty ( {A:"z", C:0, E:false} , {B:"Y", C:0, E:0} , 'E' ) ;//=false
//-------------------------------------------------------------------------
//-- Notes: for a match with cooresion change the === to ==
//-------------------------------------------------------------------------
//-- Written: 2010.01.27 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- Check to see if both Have the Property And Both of the Property
//-- Values Exactly Match without coorersion
if ( ( o1.hasOwnProperty( pn ) == o2.hasOwnProperty ( pn ) ) && ( o1[ pn ] === o2[ pn ] ) ) {
return true ;
}
//-- Check to see if both are missing the property
if ( ( ! o1.hasOwnProperty( pn ) ) && ( ! o2.hasOwnProperty( pn ) ) ) {
return true ;
}
//-- No matching
return false ;
}
//