简体   繁体   English

为什么这些对象不相等?

[英]Why are these objects not equal?

I am trying to write a test in JavaScript using Mocha and Chai. 我正在尝试使用Mocha和Chai用JavaScript编写测试。 At the end of the test, I want to compare 2 JavaScript objects, these should be equal. 在测试结束时,我想比较2个JavaScript对象,这些对象应该相等。

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? 我在这两个对象中看不到任何区别,那么为什么JavaScript会说它们不同? I am using JSON.stringify(obj1) === JSON.stringify(obj2) to compare these 2 objects. 我正在使用JSON.stringify(obj1) === JSON.stringify(obj2)比较这两个对象。

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

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? 您是否尝试过标准化密钥名称并查看比较是否有效?

您的reactions是不同的,有些包含对象,有些则没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM