JNevens
JNevens

Reputation: 11982

Why are these objects not equal?

I am trying to write a test in JavaScript using Mocha and Chai. At the end of the test, I want to compare 2 JavaScript objects, these should be equal.

The output I get from the test is the following:

{ publication: 
   { publication_id: 'pubFetch1',
     title: 'Publication Fetcher',
     person_id: 'uploader',
     URL: 'http://www.pubfetchertest.com',
     publication_year: '2015',
     upload_date: '2015-05-05 00:00:00',
     page_count: '5',
     type: 'paper',
     rating: '0',
     votes: '0',
     abstract: 'Testing the Publication Fetcher',
     location: 'location' },
  comments: 
     [ { commentID: 'comment1',
         personID: 'uploader',
         firstName: 'First',
         lastName: 'Last',
         text: 'Comment Content',
         time: '2015-05-05 10:24:36',
         reactions: [],
         myComment: true },
       { commentID: 'comment2',
         personID: 'author1',
         firstName: 'First',
         lastName: 'Last',
         text: 'Comment Content',
         time: '2015-05-05 11:01:45',
         reactions: [Object],
         myComment: false } ],
   keywords: [ 'keyword1', 'keyword2', 'keyword3' ],
   uploader: { person_id: 'uploader', first_name: 'First', last_name: 'Last' },
   score: 5,
   authors: 
     [ { person_id: 'author1', first_name: 'First', last_name: 'Last' },
       { person_id: 'author2', first_name: 'First', last_name: 'Last' },
       { person_id: 'author3', first_name: 'First', last_name: 'Last' } ],
   editors: [],
   publishers: [] }

While the object I want to compare it with, is the following:

{ publication: 
      { publication_id: 'pubFetch1',
        title: 'Publication Fetcher',
        person_id: 'uploader',
        url: 'http://www.pubfetchertest.com',
        publication_year: '2015',
        upload_date: '2015-05-05 00:00:00',
        page_count: '5',
        type: 'paper',
        rating: '0',
        votes: '0',
        abstract: 'Testing the Publication Fetcher',
        location: 'location'},
   comments: 
      [{ commentID: 'comment1',
         personID: 'uploader',
         firstName: 'First',
         lastName: 'Last',
         text: 'Comment Content',
         time: '2015-05-05 10:24:36',               
         reactions: [],
         myComment: true},
       { commentID: 'comment2',
         personID: 'author1',
         firstName: 'First',
         lastName: 'Last',
         text: 'Comment Content',
         time: '2015-05-05 11:01:45',
         reactions: [{ commentID: 'comment3',
                       personID: 'author2',
                       firstName: 'First',
                       lastName: 'Last',
                       text: 'Comment Content',
                       time: '2015-05-05 11:02:10',
                       reactions: [],
                       replyID: 'comment2',
                       myComment: false}],
         myComment: false}],
    keywords: ['keyword1', 'keyword2', 'keyword3'],
    uploader: {person_id: 'uploader',
               first_name: 'First',
               last_name: 'Last'},
    score: 5,
    authors: [{person_id: 'author1',
               first_name: 'First',
               last_name: 'Last'},
              {person_id: 'author2',
               first_name: 'First',
               last_name: 'Last'},
              {person_id: 'author3',
               first_name: 'First',
               last_name: 'Last'}],
    editors: [],
    publishers: []
};

I don't see any difference in these 2 objects, so why does JavaScript say these are different? I am using JSON.stringify(obj1) === JSON.stringify(obj2) to compare these 2 objects.

JSFiddle: http://jsfiddle.net/9akeh8bh/4/

Upvotes: 0

Views: 97

Answers (2)

Ed Knowles
Ed Knowles

Reputation: 1925

Your reactions are different, some contain Objects, some do not.

Upvotes: 0

Jonathan Smith
Jonathan Smith

Reputation: 2599

There are differences between the two objects; some keys have underscores and some are uppercase.

Have you tried standardising the key names and seeing if the comparison works?

Upvotes: 3

Related Questions