【发布时间】:2021-09-03 11:37:51
【问题描述】:
如何在两个日期范围内合并两个对象?我尝试了以下 sn-ps 但重复的日期也需要更新。但现在它只更新匹配的值。
let dateRange = [
{ Date: '2021-06-10' },
{ Date: '2021-06-11' },
{ Date: '2021-06-12' },
{ Date: '2021-06-13' },
{ Date: '2021-06-24' },
{ Date: '2021-06-25' },
{ Date: '2021-06-26' },
{ Date: '2021-06-27' }
]
let result = [
{
Type: 'Production',
Date: '2021-06-26'
},
{ Type: 'Production',
Date: '2021-06-24'
{
Type: 'Production',
Date: '2021-06-24'
},
{
Type: 'Production',
Date: '2021-06-25'
},
{
Type: 'Production',
Date: '2021-06-25'
}
]
预期结果:
[
{ Date: '2021-06-10' },
{ Date: '2021-06-11' },
{ Date: '2021-06-12' },
{ Date: '2021-06-13' },
{ Type: 'Production',
Date: '2021-06-24'
{
Type: 'Production',
Date: '2021-06-24'
},
{
Type: 'Production',
Date: '2021-06-25'
},
{
Type: 'Production',
Date: '2021-06-25'
},
{
Type: 'Production',
Date: '2021-06-26'
},
{ Date: '2021-06-27' }
]
我的做法:
let jsonObj = dateRange.map((range) => {
let result = result.find(({ Date }) => range.Date ==Date);
return { ...range, ...result };
});
但它只返回唯一值。
【问题讨论】:
标签: javascript arrays typescript object