Philippe
Philippe

Reputation: 210

Push objects from array into specific fields of another array

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

Upvotes: 0

Views: 115

Answers (1)

The Karan Pargaien
The Karan Pargaien

Reputation: 66

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")});
}

Upvotes: 1

Related Questions