简体   繁体   中英

Merging Two Arrays using Typescript or JavaScript

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' }
]

You could iterate over the collection of all records and omit the records that are already in the result (based on te day name) if they do not come from the database.

If I understood correctly your problem, here is a simplified version of it:

 const recordsNotFromDB = [{ day: "Mon", val: 1, database: false }, { day: "Tue", val: 2, database: false }, { day: "Wed", val: 3, database: false }, ] const recordsFromDB = [{ day: "Mon", val: 10, database: true }, { day: "Wed", val: 30, database: true }, ] const merged = {} for (let record of [...recordsNotFromDB, ...recordsFromDB]) { if (.(record.day in merged) || record.database) { merged[record.day] = record } } const result = Object.values(merged) console.log(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM