我有两个长度相同且具有相同键的对象。唯一的区别是这些键可能具有不同的值。问题在于,应该对两个对象进行评估的方法会中断-我花了数小时试图弄清楚这个问题。

这是originalReference

{
   "id":68,
   "article_id":12338,
   "article_reference_id":68,
   "article_reference_extraction_id":667320,
   "user_id":null,
   "file":"1238-230180-1-CE.doc",
   "revision":1,
   "order":1,
   "type":"journal",
   "preview":"Chewning B, Bylund CL, Shah B, Arora NK, Gueguen JA, Makoul G. Patient preferences for shared decisions: a systematic review. Patient Educ Couns 2012 Jan; 86(1):9-18",
   "status":1,
   "source":"Unknown",
   "data":{
      "pii":"S0738-3991(11)00114-5",
      "webciteId":null,
      "publication":[

      ],
      "conference":[

      ],
      "authors":[
         {
            "firstName":"B",
            "lastName":"Chewning"
         },
         {
            "firstName":"CL",
            "lastName":"Bylund"
         },
         {
            "firstName":"B",
            "lastName":"Shah"
         },
         {
            "firstName":"NK",
            "lastName":"Arora"
         },
         {
            "firstName":"JA",
            "lastName":"Gueguen"
         },
         {
            "firstName":"G",
            "lastName":"Makoul"
         }
      ],
      "editors":[
         {
            "firstName":"G",
            "lastName":"Makoul"
         }
      ],
      "comments":[

      ],
      "source":{
         "title":"Patient Educ Couns",
         "date":{
            "year":"2012",
            "month":"01",
            "day":null
         },
         "pages":{
            "from":"9",
            "to":"18"
         },
         "volume":"86",
         "issue":"1",
         "url":"http://europepmc.org/abstract/MED/21474265"
      },
      "accessDate":null,
      "articleTitle":"Patient preferences for shared decisions: a systematic review"
   },
   "doi":"10.1016/j.pec.2011.02.004",
   "pmid":21474265,
   "pmcid":null,
   "isbn":"",
   "created_at":"2019-04-09 10:04:53",
   "updated_at":"2019-04-09 10:04:53",
   "deleted_at":null,
   "max_revision":30,
   "extraction":{
      "id":667320,
      "article_id":12338,
      "file":"1238-230180-1-CE.doc",
      "order":1,
      "original":"Chewning B, Bylund CL, Shah B, Arora NK, Gueguen JA, Makoul G. Patient preferences for shared decisions: A systematic review. Patient Educ Couns [Internet] Elsevier Ireland Ltd; 2012;86(1):9?18. PMID:21474265",
      "doi":null,
      "pmid":null,
      "isbn":null,
      "parsed":1,
      "created_at":"2019-02-25 18:40:46",
      "updated_at":"2019-02-25 18:40:46",
      "deleted_at":null
   }
}




这是reference

{
   "id":68,
   "article_id":12338,
   "article_reference_id":68,
   "article_reference_extraction_id":667320,
   "user_id":null,
   "file":"1238-230180-1-CE.doc",
   "revision":1,
   "order":1,
   "type":"journal",
   "preview":"Chewning B, Bylund CL, Shah B, Arora NK, Gueguen JA, Makoul G. Patient preferences for shared decisions: a systematic review. Patient Educ Couns 2012 Jan; 86(1):9-18",
   "status":1,
   "source":"Unknown",
   "data":{
      "pii":" ",
      "webciteId":null,
      "publication":[

      ],
      "conference":[

      ],
      "authors":[
         {
            "lastName":"Chewning",
            "firstName":"Betty"
         },
         {
            "lastName":"Bylund",
            "firstName":"Carma L."
         },
         {
            "lastName":"Shah",
            "firstName":"Bupendra"
         },
         {
            "lastName":"Arora",
            "firstName":"Neeraj K."
         },
         {
            "lastName":"Gueguen",
            "firstName":"Jennifer A."
         },
         {
            "lastName":null,
            "firstName":" "
         }
      ],
      "editors":[
         {
            "firstName":"G",
            "lastName":"Makoul"
         }
      ],
      "comments":[

      ],
      "source":{
         "title":"Patient Education and Counseling",
         "date":{
            "year":2012,
            "month":1,
            "day":null
         },
         "pages":{
            "from":"9",
            "to":"18"
         },
         "volume":"86",
         "issue":"1",
         "url":"http://europepmc.org/abstract/MED/21474265"
      },
      "accessDate":null,
      "articleTitle":"Patient preferences for shared decisions: A systematic review"
   },
   "doi":"10.1016/j.pec.2011.02.004",
   "pmid":21474265,
   "pmcid":null,
   "isbn":"",
   "created_at":"2019-04-09 10:04:53",
   "updated_at":"2019-04-09 10:04:53",
   "deleted_at":null,
   "max_revision":30,
   "extraction":{
      "id":667320,
      "article_id":12338,
      "file":"1238-230180-1-CE.doc",
      "order":1,
      "original":"Chewning B, Bylund CL, Shah B, Arora NK, Gueguen JA, Makoul G. Patient preferences for shared decisions: A systematic review. Patient Educ Couns [Internet] Elsevier Ireland Ltd; 2012;86(1):9?18. PMID:21474265",
      "doi":null,
      "pmid":null,
      "isbn":null,
      "parsed":1,
      "created_at":"2019-02-25 18:40:46",
      "updated_at":"2019-02-25 18:40:46",
      "deleted_at":null
   }
}


请注意articleTitle中的originalReference如何输入错误,而articleTitle中的reference具有正确的标题。

下面函数的目的是确保如果originalReference之前具有键的非空/非空/定义值,则reference不应覆盖originalReference中的值。

这是同时评估两者的函数。

发生的情况是,当两个变量都明确定义时,它将引发错误TypeError: Cannot read property 'articleTitle' of undefined。不知道发生了什么。

这是函数:

evaluateEmptyValues: function(reference, originalReference) {
    var vm = this;

    console.log('reference', reference);
    console.log('Before   --> ' + reference.data.articleTitle);

    // Get keys and values of both reference objects
    referenceLength = Object.entries(reference).length;
    originalReferenceLength = Object.entries(originalReference).length;

    if (referenceLength == originalReferenceLength) {
        try {
            for (var prop in reference) {
                    if (reference[prop] != undefined || reference[prop] != null) {
                        if (typeof (reference[prop]) == 'string' && reference[prop].trim() == '') {
                            reference[prop] = originalReference[prop];
                        }

                        if (typeof(reference[prop]) == 'object' && typeof(originalReference[prop]) == 'object') {
                            console.log('reference[prop]', reference[prop]);
                            console.log('originalReference[prop]', originalReference);
                            vm.evaluateEmptyValues(reference[prop], originalReference[prop]);
                        }

                        if (Array.isArray(reference[prop]) && typeof Array.isArray(originalReference[prop])) {
                            reference[prop].forEach((item, index) => vm.evaluateEmptyValues(item, originalReference[prop][index]));
                        }
                } else {
                    reference[prop] = originalReference[prop];
                }
            }
        } catch(err) {
            console.log(err);
        }
    }

    console.log('After    --> ' + reference.data.articleTitle);
    //
    // console.log('Original --> ' + originalReference.data.articleTitle);
}

最佳答案

不应该你的电话

if (reference[prop] != undefined || reference[prop] != null) {


使用AND,像这样吗?

if (reference[prop] != undefined && reference[prop] != null) {

10-06 11:06