Home > Enterprise >  Push objects from array into specific fields of another array
Push objects from array into specific fields of another array

Time:09-30

I've a method to get the data (days, month) from the current week. I want to push this data to an array in the specific object field and later loop through this array to display it.

I really have no idea how to do this.

<div v-for="day in days" :key="day.index">
  <p>{{ day.month }}</p>
  <p>{{ day.numberDay }}</p>
  <p>{{ day.textDay }}</p>
</div>
  data() {
    return {
      // days: [{ textDay: "", numberDay: "", month: "" }]
      days: []
      
    }
  },

  methods: {
    getCurrentWeek() {
      const currentDate = moment();

      const weekStart = currentDate.clone().startOf('isoWeek');

      const days = [];

      for (let i = 0; i <= 6; i  ) {
        days.push({ textDay: moment(weekStart).add(i, 'days').format("dddd") });
        days.push({ numberDay: moment(weekStart).add(i, 'days').format("Do") });
        days.push({ month: moment(weekStart).add(i, 'days').format("MMMM") });
      }

    this.days = days
    }
  }

CodePudding user response:

You should be pushing values in days array like this. Please revert, if this solves your problem.

    for (let i = 0; i <= 6; i  ) {
days.push({textDay: moment(weekStart).add(i, 'days').format("dddd"), numberDay: moment(weekStart).add(i, 'days').format("Do"), month: moment(weekStart).add(i, 'days').format("MMMM")});
}
  • Related