javascript使用map、filter或find获取此信息



以下是示例:我的第一个阵列:

var array1= [
{"dt":"20:25","al":"my test","totalprice":4180,"curr":"INR","duration":"0:00:00","dates":"2020-12-10"},
{"dt":"22:45","al":"my test","totalprice":4180,"curr":"INR","duration":"0:00:00","dates":"2020-12-10"},
{"dt":"19:15","al":"my test","totalprice":6043,"curr":"INR","duration":"0:00:00","dates":"2020-12-12"}]

第二个阵列:

var array2 = [
{
date: '2020-12-10',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null
},
{
date: '2020-12-11',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null
},
{
date: '2020-12-12',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null
}

]

我希望日期的array2与array1匹配,并添加具有匹配日期的array1的所有相应对象作为结果,对于不匹配的日期,必须使用array2对象。日期2020-12-11已从array2中用作null,因为我们在array1中没有该日期,其他日期与array1相同。

我的代码:

finalresult = array2.map(item => item ? {...item, ...array1.filter(mrd=> mrd.dates === item.date) }: {})
console.log(finalresult)

结果:

[
{
'0': {
dt: '20:25',
al: 'my test',
totalprice: 4180,
curr: 'INR',
duration: '0:00:00',
dates: '2020-12-10'
},
'1': {
dt: '22:45',
al: 'my test',
totalprice: 4180,
curr: 'INR',
duration: '0:00:00',
dates: '2020-12-10'
},
date: '2020-12-10',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null
},
{
date: '2020-12-11',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null
},
{
'0': {
dt: '19:15',
al: 'my test',
totalprice: 6043,
curr: 'INR',
duration: '0:00:00',
dates: '2020-12-12'
},
date: '2020-12-12',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null
}
]

我需要这样的结果:

[{
dt: '20:25',
al: 'my test',
totalprice: 4180,
curr: 'INR',
duration: '0:00:00',
dates: '2020-12-10'
},
{
dt: '22:45',
al: 'my test',
totalprice: 4180,
curr: 'INR',
duration: '0:00:00',
dates: '2020-12-10'
},
{
date: '2020-12-11',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null
},
{
dt: '19:15',
al: 'my test',
totalprice: 6043,
curr: 'INR',
duration: '0:00:00',
dates: '2020-12-12'
}]

有人能帮我解决这个吗

var array1 = [
{
dt: '20:25',
al: 'my test',
totalprice: 4180,
curr: 'INR',
duration: '0:00:00',
dates: '2020-12-10',
},
{
dt: '22:45',
al: 'my test',
totalprice: 4180,
curr: 'INR',
duration: '0:00:00',
dates: '2020-12-10',
},
{
dt: '19:15',
al: 'my test',
totalprice: 6043,
curr: 'INR',
duration: '0:00:00',
dates: '2020-12-12',
},
];
var array2 = [
{
date: '2020-12-10',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null,
},
{
date: '2020-12-11',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null,
},
{
date: '2020-12-12',
al: 'my test',
totalprice: null,
curr: null,
dt: null,
duration: null,
},
];
const mapped = array2.reduce((result, item) => {
const matchedDataArr = array1.filter((data) => data.dates === item.date);
if (matchedDataArr.length) {
const itemsToAdd = matchedDataArr.map((matchedData) => {
const { date, ...rest } = item;
return {
...rest,
...matchedData,
};
});
return [...result, ...itemsToAdd]
}
return [...result, item];
}, []);
console.log('- result -', mapped);

最新更新