递归函数无法正确评估对象中的键
2019-04-11
52
我有两个对象,长度相同,键也相同。唯一的区别是这些键可能有不同的值。问题在于,本来应该评估两个对象的方法却出问题了——我花了好几个小时才弄清楚这个问题。
这里是
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
}
}
请注意,
originalReference
中的
articleTitle
有拼写错误,但
reference
中的
articleTitle
有正确的标题。
以下函数的目的是确保如果
originalReference
先前具有非空/非空/已定义的键值,则
reference
不应覆盖
originalReference
中的值。
这里是评估的函数两者。
发生的事情是,当它们都明确定义时,它会抛出错误
TypeError:无法读取未定义的属性“articleTitle”
。不确定发生了什么。
这是函数:
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);
}
2个回答
您的行
if (reference[prop] != undefined || reference[prop] != null) {
是否应该使用 AND,就像这样?
if (reference[prop] != undefined && reference[prop] != null) {
Magmatic
2019-04-11
使用这个就可以了:
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);
}
}
},
Mike K
2019-04-11