File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
1
+ // implementation of some property-based equality checking where each property of the object is compared.
2
+
3
+ function isEquivalentObj ( a : { [ key : string ] : number | string } , b : { [ key : string ] : number | string } ) {
4
+ // Arrays of property names.
5
+ const aProps = Object . getOwnPropertyNames ( a ) ;
6
+ const bProps = Object . getOwnPropertyNames ( b ) ;
7
+
8
+ // If their property lengths are different, they're different objects
9
+ if ( aProps . length != bProps . length ) {
10
+ return false ;
11
+ }
12
+
13
+ for ( let i = 0 ; i < aProps . length ; i ++ ) {
14
+ let propName = aProps [ i ] ;
15
+ // If the values of the property are different, not equal
16
+ if ( a [ propName ] !== b [ propName ] ) {
17
+ return false ;
18
+ }
19
+ }
20
+
21
+ // If everything matched, correct
22
+ return true ;
23
+ }
24
+ // However, this would still work for objects that have only a string or a number as the property.
25
+ isEquivalentObj ( { hi : 12 } , { hi : 12 } ) ; // Return true.
You can’t perform that action at this time.
0 commit comments