gpt4 book ai didi

javascript - 递归函数未正确评估对象中的键

转载 作者:行者123 更新时间:2023-11-30 19:38:48 24 4
gpt4 key购买 nike

我有两个对象,长度相同,键也相同。唯一的区别是这些键可能有不同的值。问题是应该评估两个对象的方法中断了——我花了几个小时试图弄清楚这个问题。

这是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
}
}

这是引用:

{  
"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: 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) {

关于javascript - 递归函数未正确评估对象中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55638354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com