为什么这些物体不相等?

时间:2015-05-05 09:12:16

标签: javascript json compare

我正在尝试使用Mocha和Chai在JavaScript中编写测试。在测试结束时,我想比较2个JavaScript对象,这些对象应该是相同的。

我从测试中得到的输出如下:

{ 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: [] }

虽然我想与之比较的对象如下:

{ 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: []
};

我没有看到这两个对象有任何区别,那么为什么JavaScript说这些不同?我正在使用JSON.stringify(obj1) === JSON.stringify(obj2)来比较这两个对象。

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

1 个答案:

答案 0 :(得分:3)

两个物体之间存在差异;有些键有下划线,有些是大写的。

您是否尝试过标准化键名并查看比较是否有效?