0

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
    }
  }
3
  • I do not recommend using Moment.js in 2022. Commented Sep 29, 2022 at 18:54
  • Hi kissu and thanks! What do you recommend instead? Commented Sep 29, 2022 at 20:40
  • 1
    Anything from here: momentjs.com/docs/#/-project-status/recommendations Commented Sep 29, 2022 at 20:56

1 Answer 1

1

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")});
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.