递归函数无法正确评估对象中的键

时间:2019-04-11 17:41:27

标签: javascript vue.js

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

这里是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不应覆盖{{ 1}}。

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

正在发生的事情是,当两个变量都明确定义时,它将引发错误originalReference。不知道发生了什么。

这是函数:

TypeError: Cannot read property 'articleTitle' of undefined

2 个答案:

答案 0 :(得分:1)

不是您的电话

`componentWillReceiveProps() {
if (this.props.date !== null) {
  let newDate = moment(this.props.date, "ddd, MMM DD yyyy HH:mm:ss 
Z");
  this.setState({ date: newDate });
}
....

....
....
....

<SingleDatePicker
                date={this.state.date} 
                isOutsideRange={() => false}
                onDateChange={date => this.setState({ date })} /
                focused={this.state.focused} 
                onFocusChange={({ focused }) => this.setState({ 
                focused })} 
                id="selectedDate" ,
              />`

使用AND,像这样吗?

function setColorFromSidebar() {
  var ss=SpreadsheetApp.getActive();
  var html='<input type="button" value="Red" onClick="setBackground(\'#ff0000\');" />';
  html+='<br /><input type="button" value="Green" onClick="setBackground(\'#00ff00\');" />';
  html+='<br /><input type="button" value="Blue" onClick="setBackground(\'#0000ff\');" />';
  html+='<br /><input type="button" value="White" onClick="setBackground(\'#ffffff\');" />';
  html+='<script>function setBackground(color) { google.script.run.setBackGround(color);}</script>';
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showSidebar(userInterface);
}

function setBackGround(color) {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getActiveRange();
  rg.setBackground(color);
}

答案 1 :(得分:0)

可以使用它:

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

    // 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') {

                        var length = Object.keys(reference[prop]).length;
                        for (var property in reference[prop]) {
                            if (originalReference[prop][property] != undefined) {
                                if (originalReference[prop][property] != null) {
                                    // vm.evaluateEmptyValues(reference[prop][property], originalReference[prop][property]);
                                    for (var i = 0; i < property.length; i++) {
                                        if ((reference[prop][property] == null || reference[prop][property] == '') &&
                                            originalReference[prop][property] != null) {
                                            reference[prop][property] = originalReference[prop][property];
                                        }
                                    }
                                }
                            }
                        }
                    }

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