I am struggling to merge two arrays as one. I created and get data from MongoDB using an interface.
export interface IWeeklyAssessmentData {
dayName: string;
dayDate: string;
fromDb: boolean;
}
Below is the sample data from the database
[
{ dayName: 'Sun', dayDate: datetime.datetime(2023, 1, 8, 0, 0), fromDb: 'true' },
{ dayName: 'Tue', dayDate: datetime.datetime(2023, 1, 10, 0, 0), fromDb: True }
];
I have an array of information not coming from the database in the data and computed section of the code. Here is the code snippet
data: () => ({
weekDays: [
{
dayName: "Sun",
dayDate: new Date(
new Date().setDate(new Date().getDate() - new Date().getDay() + 0)
)
.toISOString()
.slice(0, 10),
fromDb: false,
},
{
dayName: "Mon",
dayDate: new Date(
new Date().setDate(new Date().getDate() - new Date().getDay() + 1)
)
.toISOString()
.slice(0, 10),
fromDb: false,
}
]
}),
computed: {
weeklyAssessmentData(): IWeeklyAssessmentData {
return TrainWiseService.weeklyAssessment;
},
}
How do I merge the array from weeklyAssessmentData and weekDays. I want to retain the object from database if the dayName is the same. All the codes I have seen and tried seems to work.
In my computed, I have tried using this code to merge it but it gave error:
Property 'weeklyAssessmentData' does not exist on type 'CombinedVueInstance<Vue
RelatedDays() {
let dayNames = new Set(this.weeklyAssessmentData.map(dayName => dayName.assessmentDayName))
let merged = [...this.weekDays, ...this.weekDays.filter(d => dayNames.has(d.assessmentDayName))]
return merged;
},
Here is the final output I am expecting from the two arrays:
merged: [
{ dayName: 'Sun', dayDate: '2023-01-08T00:00:00.000+00:00', fromDb: 'true' },
{ dayName: 'Mon', dayDate: '2023-01-09T00:00:00.000+00:00', fromDb: 'false' },
{ dayName: 'Tue', dayDate: '2023-01-10T00:00:00.000+00:00', fromDb: 'true' }
]
TrainWiseService.weeklyAssessmentis returning? also, isRelatedDaysa method on the component? where and how is it being called?