You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何补全对象数组中与基准数组缺失的月份数据?

解决方法:合并数据库数据与全月份基准数据

嘿,我来帮你搞定这个问题!你想要把从数据库获取的data数组和包含全年12月的dummyData合并,补充缺失月份的0值数据对吧?你之前尝试用_.differenceWith没得到预期结果,是因为这个方法的用法和适用场景都不对——它是用来找两个数组的差集,而不是做数据合并填充的,而且你的参数写法也不符合lodash的要求。

为什么你的_.differenceWith用法不对?

_.differenceWith的正确语法是:

_.differenceWith(array, [values], [comparator])

它的作用是找出第一个数组中存在,但后续数组中不存在的项,且比较需要通过一个自定义的比较函数。你的写法参数顺序错误,而且这个方法本身也无法帮你生成包含所有月份的完整数组,所以不适合你的需求。

正确的解决方案

下面提供两种简单有效的方法,都是基于lodash来实现的:

方法1:遍历基准数组,匹配数据库数据

遍历dummyData(全月份基准),每个月份去data里查找对应的真实数据,找到就用真实数据,找不到就用基准的0值项:

const _ = require('lodash');

const data = [{ count: 156, monthCount: 1, year: 2018 }, { count: 165, monthCount: 2, year: 2018 }, { count: 153, monthCount: 3, year: 2018 }, { count: 63, monthCount: 6, year: 2018 }, { count: 4, monthCount: 9, year: 2018 }, { count: 116, monthCount: 10, year: 2018 } ];
const dummyData = [{ count: 0, monthCount: 1, year: 2018 }, { count: 0, monthCount: 2, year: 2018 }, { count: 0, monthCount: 3, year: 2018 }, { count: 0, monthCount: 4, year: 2018 }, { count: 0, monthCount: 5, year: 2018 }, { count: 0, monthCount: 6, year: 2018 }, { count: 0, monthCount: 7, year: 2018 }, { count: 0, monthCount: 8, year: 2018 }, { count: 0, monthCount: 9, year: 2018 }, { count: 0, monthCount: 10, year: 2018 }, { count: 0, monthCount: 11, year: 2018 }, { count: 0, monthCount: 12, year: 2018 } ];

const mergedData = _.map(dummyData, dummyItem => {
  // 匹配相同月份和年份的项
  const matchedItem = _.find(data, { 
    monthCount: dummyItem.monthCount, 
    year: dummyItem.year 
  });
  // 找到就用真实数据,否则用基准的0值
  return matchedItem || dummyItem;
});

console.log(mergedData);

方法2:用键值对优化查找(适合大数据量)

如果你的数据量比较大,每次_.find会有点慢,可以先把data转成以monthCount-year为键的对象,再遍历基准数组匹配,这样查找效率更高:

const _ = require('lodash');

const data = [{ count: 156, monthCount: 1, year: 2018 }, { count: 165, monthCount: 2, year: 2018 }, { count: 153, monthCount: 3, year: 2018 }, { count: 63, monthCount: 6, year: 2018 }, { count: 4, monthCount: 9, year: 2018 }, { count: 116, monthCount: 10, year: 2018 } ];
const dummyData = [{ count: 0, monthCount: 1, year: 2018 }, { count: 0, monthCount: 2, year: 2018 }, { count: 0, monthCount: 3, year: 2018 }, { count: 0, monthCount: 4, year: 2018 }, { count: 0, monthCount: 5, year: 2018 }, { count: 0, monthCount: 6, year: 2018 }, { count: 0, monthCount: 7, year: 2018 }, { count: 0, monthCount: 8, year: 2018 }, { count: 0, monthCount: 9, year: 2018 }, { count: 0, monthCount: 10, year: 2018 }, { count: 0, monthCount: 11, year: 2018 }, { count: 0, monthCount: 12, year: 2018 } ];

// 把data转成键值对,键为"月份-年份"
const dataMap = _.keyBy(data, item => `${item.monthCount}-${item.year}`);

const mergedData = _.map(dummyData, dummyItem => {
  const key = `${dummyItem.monthCount}-${dummyItem.year}`;
  return dataMap[key] || dummyItem;
});

console.log(mergedData);

结果说明

两种方法最终都会生成包含12个月份的完整数组,缺失的月份(比如4、5、7、8、11、12月)会自动填充count:0的基准数据,而存在的月份则保留数据库的真实count值。

内容的提问来源于stack exchange,提问作者Ashish Bhangade

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:24:16