基于Value字段比对两列表生成结果列表的JavaScript问题
问题描述
我需要比对monthList与newlist的Value字段,生成Results列表:
- 若
monthList的Value在newlist中存在,沿用newlist对应条目的字段值; - 若不存在,则生成含默认值(
IsSubmittedID、IsEligible、BackgroundCheckdone为0)的条目。
但现有JavaScript代码无法得到预期结果,相关信息如下:
原代码
this.monthList.map(sli_record => { let rec_found = this.newlist.find(vale_record =>vale_record.Value === sli_record.Value); if(rec_found) { this.Results['Value'] = sli_record['Value']; this.Results['Month'] = sli_record['Month']; this.Results['Year'] = sli_record['Year']; this.Results['IsSubmittedID'] = sli_record['IsSubmittedID']; this.Results['IsEligible'] = sli_record['IsEligible']; this.Results['BackgroundCheckdone'] = sli_record['BackgroundCheckdone']; } else { this.Results['Value'] = sli_record['Value']; this.Results['Month'] = sli_record['Month']; this.Results['Year'] = sli_record['Year']; this.Results['IsSubmittedID'] = 0; this.Results['IsEligible'] = 0; this.Results['BackgroundCheckdone'] = 0; this.Results['UserPass'] = sli_record['UserPass']; } //console.log(this.Results); return this.Results; }
示例数据
newlist=[ { Id: 0, Month: 'May', Year: 2022, Value: 'May 2022', // 注:原示例中此处为February 2022,与预期结果不符,推测为笔误 IsSubmittedID: 1, IsEligible: 1, BackgroundCheckdone: 1, }, {Id: 0, Month: 'July', Year: 2022, Value: 'July 2022', IsSubmittedID: 0, IsEligible: 1, BackgroundCheckdone: 1, }] monthList =[ {Id: 1, Value: 'August 2022'}, {Id: 2, Value: 'July 2022'}, {Id: 3, Value: 'June 2022'}, {Id: 4, Value: 'May 2022'}, {Id: 5, Value: 'April 2022'}]
预期输出
[ { "Value": "August 2022", "Month": "August", "Year": 2022, "IsSubmittedID": 0, "IsEligible": 0, "BackgroundCheckdone": 0 }, { "Value": "July 2022", "Month": "July", "Year": 2022, "IsSubmittedID": 0, "IsEligible": 1, "BackgroundCheckdone": 1 }, { "Value": "June 2022", "Month": "June", "Year": 2022, "IsSubmittedID": 0, "IsEligible": 0, "BackgroundCheckdone": 0 }, { "Value": "May 2022", "Month": "May", "Year": 2022, "IsSubmittedID": 1, "IsEligible": 1, "BackgroundCheckdone": 1 } ]
问题分析
- 复用同一对象引用:原代码始终修改并返回同一个
this.Results对象,导致最终数组中所有元素都是最后一次修改的结果,而非独立条目。 - 字段来源错误:匹配到
newlist条目时,错误地使用了monthList条目的字段,而非newlist中找到的对应条目。 - 缺失Month/Year解析:
monthList的条目没有Month和Year字段,需要从Value字符串中拆分解析。 - 性能问题:每次使用
find遍历newlist查找匹配项,时间复杂度为O(n*m),效率较低。
修正后的代码
// 先将newlist转为以Value为键的映射,提升查找效率 const newlistMap = new Map(this.newlist.map(item => [item.Value, item])); // 生成Results数组 this.Results = this.monthList.map(sli_record => { // 从Value中拆分Month和Year const [Month, YearStr] = sli_record.Value.split(' '); const Year = parseInt(YearStr, 10); const rec_found = newlistMap.get(sli_record.Value); if (rec_found) { return { Value: sli_record.Value, Month: rec_found.Month, Year: rec_found.Year, IsSubmittedID: rec_found.IsSubmittedID, IsEligible: rec_found.IsEligible, BackgroundCheckdone: rec_found.BackgroundCheckdone }; } else { return { Value: sli_record.Value, Month, Year, IsSubmittedID: 0, IsEligible: 0, BackgroundCheckdone: 0 }; } });
说明
- 使用
Map存储newlist的条目,将查找时间复杂度降为O(1),整体效率提升为O(n)。 - 每次迭代都创建新的对象,确保数组中每个条目都是独立引用。
- 从
Value字符串中解析Month和Year,适配monthList的结构。 - 匹配时直接使用
newlist中找到的条目字段,符合需求。
内容的提问来源于stack exchange,提问作者Drake
相关产品推荐
相关产品推荐

